Align image and text on textview
Asked Answered
M

2

1

In my app, I populate a linearlayout with a text and a little icon (like a listview with a custom adapter). I need the text and the picture to have the same size. Is there a way to do that?

Here's the code:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
              LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
      LinearLayout LayoutPadre = new LinearLayout(this);
TextView Tag = new TextView(this);
      ImageView img = new ImageView(this);

      Tag.setText(data);
     if(stato.equalsIgnoreCase("presente")){
      img.setImageResource(R.drawable.ic_presente); 
     }else{
         img.setImageResource(R.drawable.ic_assente);  
     }

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(75, LayoutParams.WRAP_CONTENT);   //Here I need the img to have same size as the text, but Tag.getHeight() returns 0
      layoutParams.setMargins(50, 0, 0, 0);

      img.setLayoutParams(layoutParams);

      LayoutPadre.setLayoutParams(params);

      LayoutPadre.addView(Tag);
      LayoutPadre.addView(img);

The problem is that the image is not horizontally centered like the img. Look at the screenSreen capture

Thanks to all.

Midkiff answered 24/6, 2014 at 15:3 Comment(0)
A
2

keep the layout_height of text and image as Match_Parent and not Wrap_Content... By that

Assignation answered 24/6, 2014 at 15:22 Comment(1)
thank's! it work!! i mak ea lot of attempt, but not thant..Thanks again!Midkiff
G
0

If you are preserving the android:layout_height="wrap_content" or it did not work, you can also do android:gravity="center"

Gizmo answered 15/3, 2019 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.