I want to change my view's height based on the screen size. say for a 3" screen it has to be 60dp and for a 5" screen it has to be 100dp. How can I get this?
You can get Screen dimension(Height and width) of device then you can set Height and width of Element (Button/Edittext/LinearLayout), But remember it require a right calculation for displaying an element in right position.
you could do something like this:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x; // int screenWidth = display.getWidth(); on API < 13
int screenHeight = size.y; // int screenHeight = display.getHeight(); on API <13
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(something * screenWidth, something * screenHeight); // This should set the witdh and height of the TextView
lp.setMargins( something * screenWidth, something * screenHeight, 0, 0); // This serves as the settings for the left and top position
txtV.setHeight(something * screenHeight);
txtV.setWidths(something * screenWidth);
txtV.setLayoutParams(lp);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With