Abhijeet Abhijeet -4 years ago 86
Android Question

i want to generate programitically Horizontal scrolling LinearLayout with Imageview and textview at center bottam

i want to generate programitically Horizontal scrolling LinearLayout with Imageview and textview at center bottam.

this is my Java code.:

LinearLayout rec=(LinearLayout)findViewById(R.id.hori_recom);

//ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(450,450);
ViewGroup.LayoutParams params1=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
// for(int g=0;g<5;g++)
for(int g=0;g<imgpath_orignal.size();g++)
{

ImageView recimg=new ImageView(Details.this);
recimg.setId(g+1);
recimg.setPadding(25,0,0,0);
Picasso.with(Details.this).load(al_gallary_img.get(g)).placeholder(R.drawable.logo)
.error(R.drawable.logo).into(recimg);
recimg.setLayoutParams(params);
TextView txtlabel=new TextView(Details.this);
txtlabel.setId(g+1);
txtlabel.setGravity(Gravity.CENTER|Gravity.BOTTOM);
txtlabel.setPadding(15,15,15,15);
txtlabel.setText(""+al_img_caption.get(g));
txtlabel.setLayoutParams(params1);
rec.addView(txtlabel);
rec.addView(recimg);


This is my Xml :

<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/hori_recom"
android:layout_marginTop="10dp"/>

</HorizontalScrollView>


Problem is i am not getting my textview at bottom-center of my Imageview.please help

Answer Source

This is because you have set gravity of TextView as center_bottom instead of layout_gravity.

Just remove

txtlabel.setGravity(Gravity.CENTER|Gravity.BOTTOM);

and instead of

ViewGroup.LayoutParams params1 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);`

do this

LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); params1.gravity = Gravity.CENTER|Gravity.BOTTOM;

This is the way of setting layout_gravity programatically.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download