How to make the application look the same on all devices?

I have been developing Android recently, and I am creating an application in the Android studio, and I want my application to have the same design on all devices:

Samsung Galaxy S4 => 1080x1290; 5.0 "

Galaxy Nexus => 720x1280; 4.7 "

Nexus 4 => 768x1280; 4.7 "

Motorola Droid Razr M => 540x960; 4.3 "

Nexus S => 480x800; 4"

Galaxy S2 => 480x800; 4.3 "

Galaxy Ace => 320x480; 3.5 "

Galaxy Note => 800x1280; 5.3 "

Galaxy Note II => 720x1280; 5.5 "

Nexus 10 => 2560 x 1600; 10.1 "

Galaxy Tab 10.1 => 1280 * 800; 10.1 "

Galaxy Note 8.0 => 1280 * 800; 8.0 "

Galaxy Tab 7.7 => 1280 * 800; 7.7 "

Nexus 7 => 1280 * 800; 7.0 "

Galaxy Tab => 1024 * 600; 7.0 "


and I read these and many questions here

http://developer.android.com/training/multiscreen/index.html

http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts

I used width / height in "dp" and did it with all the elements

android:layout_margin android:paddingLeft android:paddingRight android:paddingTop android:paddingBottom 

and it works fine, but since I know that there is a method that reads the device’s screen, then decide which layout will open in the application, suppose I have one layout and I have these sizes for it

res / layout / main_activity.xml # For mobile phones (available width less than 600dp) res / layout-sw600dp / main_activity.xml # For 7-inch tablets (600dp wide and more) res / layout-sw720dp / main_activity.xml # For 10 inch tablets (720dp wide and larger)


My question is: how do I code my Java activity to read the screen of the device my application is running on and then decide which layout will be open, as if the device was S2, then java will do something like this:

 if (tabletDeviceSize) { //use tablet support layout } else { //another layout for mobile support } 

I see this code, but I need the full one, because I'm not perfect in coding :(

 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); switch(metrics.densityDpi){ case DisplayMetrics.DENSITY_LOW: break; case DisplayMetrics.DENSITY_MEDIUM: break; case DisplayMetrics.DENSITY_HIGH: break; } 
+5
source share
2 answers

According to the official documentation, you do not need to grammatically decide which layout to use with the appropriate screen size.

To optimize the application user interface for different screen sizes and densities, you can provide alternative resources for any of the generic sizes and densities. As a general rule, you should provide alternative layouts for some different screen sizes and alternative bitmaps for different screen densities. At run time, the system uses the appropriate resources for your application based on the total size or screen density of the current device.

In other words, if you follow the recommendations outlined in the documentation, as I see it, you did it by placing the layout files in their respective resource folder as follows:

 res/layout/main_activity.xml # For handsets (smaller than 600dp available width) res/layout-sw600dp/main_activity.xml # For 7" tablets (600dp wide and bigger) res/layout-sw720dp/main_activity.xml # For 10" tablets (720dp wide and bigger) 

Then the system will decide which layout to use. Additional code is not required to indicate it at runtime.

If you want to make changes depending on the screen resolution, you can get the width and height in pixels using the following code

 Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; 

Then do something cutting-off depending on the variables width and height , for example. in your case with S2:

 if(width == 480 && height == 800){ //Do work that related to the S2 screen resolution }else if(...){ } 
+10
source

If you ask how to get the device’s screen density, it took me 3 seconds to find this answer (not including the time to enter “Android device density” in my favorite search engine):

programmatically adjust the screen density in android?

+1
source

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


All Articles