0

I've been trying to set the height of an ImageView programmatically in Java, but when I launch it, it just disappears.

The code is similar to the following

ImageView img = (ImageView)findViewById(R.id.image);
img.setImageResource(picture[i];
DisplayMetrics dm = new DisplayMetrics();
int width = dm.widhtPixels;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, width/3);
img.setLayoutParams(layoutParams);

Maybe I have to append that the image and another text (which isn't mentioned in the code) are given to a CustomAdapter to create a ListView.

Thank you for your help :-)

user2177480
  • 53
  • 1
  • 6

2 Answers2

2

I just tested your code. The issue is to do with the width param and how you've attained it. The code to set the parameters will work if you pass literal values

If you try this (I know this isn't what you want, bear with me)

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(150, 50);
img.setLayoutParams(layoutParams);

You will see that it works (you shouldn't use literals like this, at least not without scaling them to DP), however this is not what you want, as you want to do it dynamically off of the width.

There is apparently an issue of getting width and height in onCreate(), see this question and the corresponding answers How to get the width and height of an Image View in android?

Hopefully this narrows down your search.

Community
  • 1
  • 1
pfairbairn
  • 451
  • 2
  • 8
  • Yes, you're right that i want to do that dynamically. But I think that question you posted handles something different. Because I want to make the width and the height of the ImageView depending on the screen size of the given device. In the end, the View should fill the whole width of the screen and the height should be adapted which would be width/3. – user2177480 Aug 30 '13 at 14:34
  • I was more trying to highlight that the issue is with the width value, not your code to apply the layout params. You need to find a concrete way of getting the value of the width of the screen, then the rest will work, as it appears doing it in onCreate() is still causing issues. You may need to do it after OnCreate() somehow. – pfairbairn Aug 30 '13 at 14:40
  • Thank you :-) any ideas how i could achieve this? – user2177480 Aug 30 '13 at 14:43
  • This answer shows numerous ways to get the width http://stackoverflow.com/a/1016941/2728623 and http://stackoverflow.com/a/4847027/2728623 however you will have to take care with the width size. Simply dividing by 3 will not be accurate when translating layout parameters with it, you'll maybe have to do some conversions and work it out, how this will work to get the width parameter from the display metrics. – pfairbairn Aug 30 '13 at 14:50
0

What are you trying to achieve, is the image always 1/3 of the screen width?

If so, you could do this quite easily using layout weight in your layouts. Set weightSum=3 on the parent and then use a layout_weight=1 on the image view(s).

Philio
  • 3,675
  • 1
  • 23
  • 33
  • I am trying to achieve that the ImageView fills the whole width of the screen and the height should be 1/3 of the width because the width-to-height-ratio of the picture is 3:1 – user2177480 Aug 30 '13 at 14:18