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.

Gravitoid
  • 1,294
  • 1
  • 20
  • 20
Lele
  • 703
  • 3
  • 15
  • 34

2 Answers2

2

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

Siva
  • 9,043
  • 12
  • 40
  • 63
0

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