Reputation: 2712
I am creating a app in Android that must support multiple screens.
For this purpose i have read many answers and one which i like most is state that i can create different folders in res folder by following name,
For small screens layout-small,
For large screens layout-large,
For extra large screens layout-xlarge
and i have to create different layouts for different screens with same name. e.g mylayout.xml
and i have to put following code in manifest.xml file
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
and finally i just need to write following lines in my Activity file,
setContentView(R.layout.mylayout);
Now when i run this app in different size screens it should acquire layouts from different folders as per screen resolution.
But this is not happening in my case.It takes only layout that defined in layout folder.
Please can some one explains me why this happening and how can i solve this issue so that my app can run effectively on all size screens.
Upvotes: 10
Views: 30687
Reputation: 4382
Try out like:
instead of
Upvotes: 17
Reputation: 441
Here is a quick checklist about how you can ensure that your application displays properly on different screens:
Use wrap_content, fill_parent,
or dp
units when specifying dimensions in an XML layout file.
Do not use hard coded pixel
values in your application code(.java
files).
Do not use AbsoluteLayout
(it's deprecated in Android 1.5
). You should instead use RelativeLayout
, which uses relative positioning to lay out its child views.
Supply alternative bitmap drawables for different screen densities.
Take time and read these screens_support or to get a better idea see How Android Finds the Best-matching Resource so that you know where to place your resources.
Upvotes: 1
Reputation: 428
Please refer below link:
http://developer.android.com/guide/practices/screens_support.html For Different screen size, The following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for small, medium, high, and extra high density screens. you could use different size of the layout files in res folder and also vary for drawable images based on the density..
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
<compatible-screens>
<screen
android:screenDensity="ldpi"
android:screenSize="small" />
<screen
android:screenDensity="mdpi"
android:screenSize="normal" />
<screen
android:screenDensity="xhdpi"
android:screenSize="large" />
<screen
android:screenDensity="xhdpi"
android:screenSize="xlarge" />
</compatible-screens>
And followed by any activity use this lines..
android:configChanges="orientation|screenSize|keyboardHidden"
Upvotes: 8
Reputation: 8849
In values folder naming convention like layout-small
only works for devices with api version less than 3.1
. You should create values file with naming like layout-sw600dp
for api level greater than 3.1. read this http://developer.android.com/guide/practices/screens_support.html3.1api
like this you should create layout-sw600dp, layout-sw720dp for each type of devices. layout-sw600dp means this layout works for devices with smallest width of 600dp If you have layout-600dp and layout-sw720dp folders. first layout folder works for devices with smallest width of 600dp(7 inch tablet) to 720dp and second works for devices with smallest width above 720dp(10 inch tablet).
If your minimum required version is above 3.1 you don't need have layout-small, layoutxLarge folders. otherwise you have to consider both type of layout fromats.
Upvotes: 4