0

i want to my apps support different screen sizes. i add folders "layout-small and layout-large " in /res directory. but XMLs inside this folders aren't accessible in my activity.so i add all my XMLs in default layout and add this code

if((getResources().getConfiguration().screenLayout && 
      Configuration.SCREENLAYOUT_SIZE_SMALL) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
          setContentView(R.layout.main1);
    }else if((getResources().getConfiguration().screenLayout &&
                Configuration.SCREENLAYOUT_SIZE_LARGE) == Configuration.SCREENLAYOUT_SIZE_LARGE){
                     setContentView(R.layout.main2);
        }
        else
            setContentView(R.layout.main);

in my activity, but when my AVD skin is 1024*600 and hw.lcd.dencity is 160 (large) it didn't work.

any help?

Jeetu
  • 686
  • 2
  • 10
  • 20
sepehri
  • 1
  • 1
  • 4

5 Answers5

2

Size: small, normal, large
Density: ldpi, mdpi, hdpi,
nodpi(no auto-scale) Aspect ratio: long, notlong
Orientation: land

Usage:

res/layout/my_layout.xml            
res/layout-small/my_layout.xml      
res/layout-large/my_layout.xml      
res/layout-large-long/my_layout.xml      
res/layout-large-land/my_layout.xml     
res/drawable-ldpi/my_icon.png  
res/drawable-mdpi/dpi/my_icon.png  
res/drawable-hdpi/my_icon.png      
res/drawable-nodpi/composite.xml   

Restricting your app to specific screen sizes(via the AndroidManifest):

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<supports-screens
          android:largeScreens="true"
          android:normalScreens="true"
          android:smallScreens="true"
          android:anyDensity="true" />
...
</manifest>

And for code level tweeking:

float scale = getContext().getResources().getDisplayMetrics().density;

And don't forget:

dpi    = 160; //At 160dpi
pixels = dips * (density / dpi)

Also refer support multiple screen in android

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
  • thanks.what is the meaning of float scale = getContext().getResources().getDisplayMetrics().density; and dpi = 160; //At 160dpi pixels = dips * (density / dpi) – sepehri Jan 08 '13 at 13:30
1

Layout name must be same

layout-small \ main1.xml
layout-normal\ main1.xml
layout-large \ main1.xml

You dont need to handle this, android automatically decide which layout will be used

setContentView(R.layout.main1);
Talha
  • 12,673
  • 5
  • 49
  • 68
1

Please look at this:

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

res/layout/my_layout.xml         // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml      // layout for extra large screen size

res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

res/drawable-mdpi/my_icon.png        // bitmap for medium density
res/drawable-hdpi/my_icon.png        // bitmap for high density
res/drawable-xhdpi/my_icon.png       // bitmap for extra high density

more read so please see this link How to support different screen size in android

Community
  • 1
  • 1
Mahesh Kavathiya
  • 573
  • 5
  • 23
0

Android automatically does this for you. For the different screen sizes, make different xml files, but give them the same name, for example main.xml and put the one for large in the folder /res/layout-large, for small in /res/layout-small, et cetera.

in the onCreate(), just put setContentView(R.layout.main)

For more information, consider reading this site from Android Developers.

stealthjong
  • 10,858
  • 13
  • 45
  • 84
0

Android will automatically pick up the best resource for a given particular device.If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density.

The "default" resources are those that are not tagged with a configuration qualifier.

Shinoo Goyal
  • 601
  • 8
  • 10