Android layout management

In fact, I created an application for all screen resolution. Therefore, for this, according to the documentation, I created a list of resource directories in the application, which provides various layout schemes for different screen sizes and different bitmap images for medium, high and high density screens.

For instance:

res/layout-normal/my_layout.xml // layout for the normal screen size ("default")

res/layout-small/my_layout.xml // layout for a small screen size

res/layout-large/my_layout.xml // layout for a large screen size

res/layout-xlarge/my_layout.xml // layout for an especially large screen

Now, when I run my application on another device, I noticed that some devices with different screen resolutions accept the layout from the same resource directories as the normal layout, and an example of such devices:

HVGA (320 x 480)

WQVGA 400 (240 x 400)

WVGA (480 x 800)

WXGA (720 x 1280)

Due to the use of the layout from the same resource directories, that is, it is very difficult for me to manage the space between the user interface for all devices, since they accept the same layout. Because if I control the layout for the HVGA, then it will not look good in another, because it is a resolution.

So, is there any way to solve this problem. Please help me solve this problem.

+4
source share
3 answers

Do not create xml for each layout. just make images for everyone (ldp, mdpi, hdpi) with the same name and put in this different folder according to size.

 ldpi : 240X320 mdpi : 320X480 hdpi : 480X800 

And give permission in the Android Android manifest file.

  <supports-screens android:normalScreens="true" android:anyDensity="true" android:largeScreens="true" android:smallScreens="true" /> 

I use this method to make the universal application in android.it work fine, try like this.

0
source

@hasMukh pointed in the right direction, but if you want the layouts for these 4 devices to be more accurate. I suggest you use the layout-density-resolution format for naming layout folders. As an example,

for HVGA (320x480) the layout should be "layout-hdpi-480x320", for the WQVGA (240x400) folder the layout should be "layout-ldpi-400x240", for WVGA (480x800) the layout folder should be "layout-mdpi-800x480" for WXGA (720x1280) the layout folder should be "layout-mdpi-1280x720"

This method is only good if you plan on targeting several specific devices .. and you can save only one drawabale folder and use dp values ​​for layouts.

0
source

I have the same problem, I used a different approach. get device height / width:

 DisplayMetrics displayMetrics = new DisplayMetrics(); WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut wm.getDefaultDisplay().getMetrics(displayMetrics); screenWidth = displayMetrics.widthPixels; screenHeight = displayMetrics.heightPixels; 

and check the height / width of the device according to what I used layout, e.g.

 if(screenWidth == 320 || screenHeight== 480){ setContentView(R.layout.test_320_480); }else if(screenWidth == 240 || screenHeight == 320){ setContentView(R.layout.test_240_320); }else if(screenWidth == 480 || screenHeight == 800 || screenHeight == 854){ setContentView(R.layout.test_480_800); } 

use the same identifier for each control in the layout for the same screen. hv put all the layout in the layout folder and it works for me.

-2
source

Source: https://habr.com/ru/post/1402607/


All Articles