sandmaster
sandmaster

Reputation: 65

Android: How to access different layouts depending of screen density in XML

I am wondering how to get different XML layout files to be read depending of density of the screen.

Now when I enter a layout file and set to so that it has a couple of buttons and have them set to a specific position, the position changes when I change to another screen density in the emulator. This is what should be happening what i understand- but How do I get the program to use different layouts depending of densities?

I have been reading on android dev. page on how to develop for different screens, but I didn't get the examples which were there.

I have been using dp/dip on sizes so no need to suggest that =)

Upvotes: 2

Views: 3994

Answers (2)

Henry Thompson
Henry Thompson

Reputation: 2481

By placing the layout XML files in different folders you can get Android to load the correct one depending on the screen density. For example, if you layout is called "main.xml":

  • Placing the file in /res/layout-ldpi/main.xml will mean it is used only in low density (or above)
  • Placing the file in /res/layout-mdpi/main.xml will mean it is used only in medium density (or above)
  • Placing the file in /res/layout-hdpi/main.xml will mean it is used only in high density (or above)
  • Placing the file in /res/layout-xhdpi/main.xml will mean it is used only in extra-high density

You can do the same sort of thing with all resources- drawables, strings, dimensions etc. (e.g. drawable-mdpi, or values-en [values only to be used in an English locale]). You can find the whole list of different device set-ups supported by this system at http://developer.android.com/guide/topics/resources/providing-resources.html (Table 2: Configuration Qualifier names)

Upvotes: 3

Okan Kocyigit
Okan Kocyigit

Reputation: 13441

you can do it by creating a layout for each standart screen sizes,

you can find here how to do it.

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

it means you should create sub-directory named as ("layout", "layout-small","layout-large","layout-xlarge","layout-xlarge-land") in res folder.

than create a main xml for each one with the same name.

Upvotes: 5

Related Questions