I implemented my own way of handling multiple screen resolutions .
You could avoid this problem by setting LayoutParams at runtime in terms of Percentage
Problem occurs only with Views/Layouts , which has some constant width or height allows 280dp . The solution is quite simple if we programmatically set the Layout Parameters of our Views/Layouts in terms of Percentage and use only the width or height constant, where necessary, try using match_parent to fill in the empty space or using weights and define each View compared to the other Views , this will help your layout look good in almost all screen resolutions
Here is an xml example
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/mLayout" android:layout_width="280px" android:layout_height="300px" /> </RelativeLayout>
Note: I used px for a fixed width / height layout, because in LayoutParams layoutParams = new LayoutParams(int width, int height); width and height read pixel value
Here is an example installation code Width and Height as a percentage
final ViewTreeObserver mLayoutObserver = mLayout.getViewTreeObserver(); mLayoutObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { DisplayMetrics metrics = getResources().getDisplayMetrics(); int deviceWidth = metrics.widthPixels; int deviceHeight = metrics.heightPixels; float widthInPercentage = ( (float) 280 / 320 ) * 100; float heightInPercentage = ( (float) 300 / 480 ) * 100; int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth) / 100 ); int mLayoutHeight = (int) ( (heightInPercentage * deviceHeight) / 100 ); LayoutParams layoutParams = new LayoutParams(mLayoutWidth, mLayoutHeight); mLayout.setLayoutParams(layoutParams); } });
Now, perhaps some people are wondering what is going on here.
float widthInPercentage = ( (float) 280 / 320 ) * 100
Let me explain 280 - the width of my LinearLayout and 320 - is the width of the screen of my device (which I am developing), I know that I am currently testing a device with a resolution of 320 x 480 , what I am doing is calculating how much area my layout covers in terms of percentage and then
int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth) / 100 )
Here I calculate the new width for my layout according to the screen resolution, and this way your Views/Layouts will look exactly the same at every screen resolution .
Conclusion: If you need to set the width / height constant for your Views/Layouts always set value in px in the layout file (i.e. xml), and then programmatically set LayoutParams .
Suggestion for Google Android developers , I think you guys should seriously consider changing dp/dip units to percentage