Home » Android » Different Layouts For Different Screen Sizes On Android?

Different Layouts For Different Screen Sizes On Android?

Posted by: admin June 15, 2020 Leave a comment

Questions:

So I made an app using in Eclipse using the Graphical Editor, AbsoluteLayout, fixed pixel values, etc… just bad practice in general. It defaulted to a 3.7in screen. Is there any way to design a separate layout for each screen size and have the program choose which to load based on said screen size?

How to&Answers:

Provide different layouts for different screen sizes
By default, Android resizes your application layout to fit the current device screen. In most cases, this works fine. In other cases, your UI might not look as good and might need adjustments for different screen sizes. For example, on a larger screen, you might want to adjust the position and size of some elements to take advantage of the additional screen space, or on a smaller screen, you might need to adjust sizes so that everything can fit on the screen.
The configuration qualifiers you can use to provide size-specific resources are small, normal, large, and xlarge. For example, layouts for an extra large screen should go in layout-xlarge/.

Beginning with Android 3.2 (API level 13), the above size groups are deprecated and you should instead use the sw<N>dp configuration qualifier to define the smallest available width required by your layout resources. For example, if your multi-pane tablet layout requires at least 600dp of screen width, you should place it in layout-sw600dp/. Using the new techniques for declaring layout resources is discussed further in the section about Declaring Tablet Layouts for Android 3.2.

From the Android developer site – just use layout-small/ layout-xlarge/ etc.. folder :=)
best way ever..

Answer:

I fixed this by running this code at startup

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();

From there I do an if statement to check for resolution and load a specific layout made for that resolution. EX. WVGA screen is 800×480 so I check for that and load the layout.

if (width == 480 && height == 800)
    {
        setContentView(R.layout.main);
    }

This was for API < 13, now those functions are deprecated, see this Get screen dimensions in pixels

Answer:

Instead just make different folders in the “res” folder,
“layout-small” for small screens,
“layout-large” for large screens,
“layout-xlarge” for extralarge screens.
Design the layouts accordingly.click here for more info..