Java imageview height based on screen size

www.la‮u‬tturi.com
Java imageview height based on screen size

To set the height of an ImageView in Java based on the screen size, you can use the getResources().getDisplayMetrics().heightPixels method to get the screen height in pixels, and then use the setLayoutParams method to set the height of the ImageView as a proportion of the screen height.

Here's an example of how you might do this:

// Get the screen height in pixels
int screenHeight = getResources().getDisplayMetrics().heightPixels;

// Set the height of the ImageView to half the screen height
ImageView imageView = findViewById(R.id.image_view);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, screenHeight / 2));

This code sets the height of the ImageView to half the screen height. You can adjust the proportion as needed to achieve the desired height.

Keep in mind that this approach is specific to Android and will not work in a pure Java application. If you are working on a Java application, you will need to find a different way to determine the screen size and set the ImageView height accordingly.

Created Time:2017-11-03 23:27:12  Author:lautturi