Pavel Oganesyan
Pavel Oganesyan

Reputation: 6924

Image with fixed size in procents on Android

I have a bunch of images with different sizes. Each of them should be presented on the top part of the screen and must take the space in height equal to 60% of screen height. Width of the image will be dependent on it's height to save initial proportions. I tried to use weightsum property in layout and weight property in ImageView, but I don't know what to put in the height property of my image view. If it is "wrap_content", every image resizes my ImageView and all mark-up crushes. Any advices?

Upvotes: 0

Views: 99

Answers (1)

Givi
Givi

Reputation: 3283

If you want, you can set the image's dimensions by code. Just set the width with the weight_sum method and then do something like:

 WindowManager manager = (WindowManager) context.getSystemService(Activity.WINDOW_SERVICE);
 int screenHeight = manager.getDefaultDisplay().getHeight();
 YOUR_VIEW.getLayoutParams().height = (int) (screenWidth * 0.6);

Please mind that you can do so only AFTER your ImageView has been drawn on screen - so calling it within the onCreate() method will not work. You can either call it delayed (postDelayed) or set a layout listener to one of your view.

Hope this helps.

Upvotes: 1

Related Questions