Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different Layouts For Different Screen Sizes On Android?

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?

like image 781
Cistoran Avatar asked Jun 27 '11 06:06

Cistoran


2 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..

like image 55
cV2 Avatar answered Sep 20 '22 15:09

cV2


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 800x480 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

like image 31
Cistoran Avatar answered Sep 18 '22 15:09

Cistoran