0

I am creating a simple layout in android with some customized buttons(not the ones android provides by default).The image appears proper on screen size 2.7 to 5.1. However buttons appear stretched on screen size 5.4 and greater (tried till screen size 7).I have made four same images named them similarly of different densities and placed them correspondingly in the folders ldpi,mdpi,hdpi,xhpi.I need to know whether i need to create different layouts for different screens?Or android picks up images automatically from the corresponding folders?Some body please guide me as i am new to android on this as i am struggling to create layouts targeting android mobile devices.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Raj
  • 217
  • 1
  • 6
  • 15
  • possible duplicate of [multiple screen support in android](http://stackoverflow.com/questions/7453982/multiple-screen-support-in-android) – arpit Aug 06 '15 at 06:06

5 Answers5

4

No need of defining different layout folders(layout-land, layout-large, layout-small) if u designed layouts consistently. The Android OS will take care of that(regarding images display). Only thing you have to do is add the <support-screens> in your manifest and below is the <support-screens>:

 <supports-screens android:resizeable=["true"| "false"]
              android:smallScreens=["true" | "false"]
              android:normalScreens=["true" | "false"]
              android:largeScreens=["true" | "false"]
              android:xlargeScreens=["true" | "false"]
              android:anyDensity=["true" | "false"]
              android:compatibleWidthLimitDp="integer"
              android:largestWidthLimitDp="integer"/>

Tips for Creating consistent layouts:

  1. Dont hard-code any layout parameters such as width,height,etc..
  2. Dont use "px".Use "sp" for Text Size and "dp" for layout-width, layout-height etc.
  3. Make use of RelativeLayout and LinearLayout and dont use AbsoluteLayout as it is deprecated.
  4. Use ScrollView wherever required for layouts as it supports for a singleView.

For more information check the Android Developer documents of Support Multiple Screens.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
  • One more question how can i change the text size of text within those components(Edittext) and what will be the ratio of the images dimension? – Raj Feb 14 '13 at 07:16
  • declare the textsize as "sp" instead of px. For example, use android:textSize="18sp" instead of android:textSize="18px". sp is independent of screen size/density. I have edited my answer. Please check. – Avadhani Y Feb 14 '13 at 07:18
  • ,Could u please provide some sample app to actually try and run the app to display android support multiple screen.I havent found any sample app.It would benefit others as well.Also is it necessary that the images should be in proportion od 3:4:6:8? – Raj Feb 14 '13 at 12:35
3

You should adopt writing different layouts xml for different screen sizes and put them into the res folder.

For approaching different densities due to Android versions one can go about like this

res/layout/mylayout.xml       # Default layouts
res/layout-v4/mylayout.xml    # Android 1.6 layouts
res/layout-v11/mylayout.xml   # Android 3.0 layouts

while for different screen sizes you need some layouts like these:

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)

More info about these different layouts (qualifiers) can be found here.

Go through this question and its answers too.

Note: always use relative layouts when handling different screen sizes; it's a plus.

Edit

To get to know more about resource qualifiers these links are good.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nezam
  • 4,122
  • 3
  • 32
  • 49
  • I would avoid creating different layouts for different screens. Imagine extending or modifying your app layout later, when you have dozens of layouts already. You will have to go through the all of them and add some new elements. Better way would be to have one layout but make sure that the layout uses customizable paddings and margins. Then go ahead and create multiple attr.xml/dimen.xml files for each screen configuration and re-define your paddings and margins there as needed. Simply put them into values-sw600dp and so on... – avepr Jun 03 '14 at 05:21
1

The following link will help you understanding Supporting multiple screens in android: http://developer.android.com/guide/practices/screens_support.html

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
  • Can somebody give me a link /an example (sample app) how to create layouts for multiple screens. – Raj Feb 14 '13 at 07:05
  • @joy it's all written on the link provided. Have you read that? name the layout folders according to requirement, put xml corresponding files into them and they will work. – Usama Sarwar Feb 14 '13 at 07:11
1

If you have different pictures in all the density folder, android will automatically take from the corresponding folder. make sure that you are using nine-patch image in the density folder.

Have a look of Supporting Multiple Screens in Android

kumar_android
  • 2,273
  • 1
  • 21
  • 30
1

use this in manifest.xml

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

How to solve Android screen size for different mobile devices?

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

Community
  • 1
  • 1
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98