0

I want to generate programmatically Horizontal scrolling LinearLayout with Imageview and textview at center bottom.

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.

2 Answers 2

0

Do one thing - combine your ImageView and TextView in one layout
Say this layout as row_layout.

<LinearLayout
    ....
    ....

    <ImageView
        ...
        ... />
    <TextView
        ...
        ... />
</LinearLayout>

Now add Layout into the LinearLayout which lies into HorizontalScrollView -

for(int g=0; g<imgpath_orignal.size(); g++) {
    View v = LayoutInflater.from(context).inflate(R.layout.row_layout, parent, false);
    // Do your layout binding stuff over here..
    ImageView ivPhoto = (ImageView) v.findViewById(R.id.imageview_id); // Give reference
    Picasso.with(context).load(url).into(ivPhoto); // Loading image using Picasso
    rec.addView(v);
}
3
  • Sir I am new in android development will you please suggest how to bind Imageview and Textview to new inflated layout.
    – Abhijeet
    Mar 23, 2017 at 5:53
  • I have done something like this : LayoutInflater inflater = Details.this.getLayoutInflater(); View rowView = inflater.inflate(R.layout.row_layout, null, true); LinearLayout view_custom=(LinearLayout)findViewById(R.id.view_custom); view_custom.addView(recimg); view_custom.addView(txtlabel); but it crash
    – Abhijeet
    Mar 23, 2017 at 5:57
  • See how I did findViewById() stuff, I've edited the code.
    – Paresh P.
    Mar 23, 2017 at 6:02
0

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.

1
  • It give's output as textview at bottam but not textview below imageview.
    – Abhijeet
    Mar 23, 2017 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.