Supporting all screen sizes in android? - android

I know this questions has been asked quite a few times. But I couldn't find the bebest possible solution to my query.
I have followed every step given in the developer support of android.
Support Multiple Screen
Putting images in different folders for different sizes.
Making different layout folders for device screens.
But the problem persist in the devices ranging from 4.7 inches to 5.0 inches.
The layout gets quite disturbed on these devices.
What is the best and possible way to overcome that.

Basically some devices belongs to large or normal group but they have so much difference in height and width for example- Nexus-4 4.7" having dimensions 768x1280 and one other device 5.1" having dimensions 480x800. So you can create layouts folders depending upon height and width. For example- layout-w480dp, layout-w720dp or layout-h800dp or layout-h1280dp. Then set views in those layouts according to your requirement.

If you are designing any forms go for Match Parent, dont go for hardcore pixel positions. Use Relative layout.
P.S Please tell more about your compatibility issues?

Please refer these link:
Multipal screen size handling!
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.
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
The following code in the Manifest supports all dpis.
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />

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
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="false"
android:xlargeScreens="true"
/>
<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"

If you need an absolute measure of your the screen's density you can use the following code:
Got this from somewhere sometime ago, but still relevant. Enjoy it !
DisplayMetrics metrics = new DisplayMetrics();
try {
WindowManager winMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE)
;
winMgr.getDefaultDisplay().getMetrics(metrics);
}
catch (Exception e) {
metrics.density = 1;
}
The value metrics.density now contains a measure of the screen's density with 160dpi density as a the 'baseline'. More info can be found here:
http://developer.android.com/reference/android/util/DisplayMetrics.html#density

Related

Android supporting multiple resolution with multiple layout folder

Now I'm supporting multiple resolution with multiple layout folders.
I'm using android development studio and I made 3 different folders.
layout
layout-large-port-1280x720
layout-normal-port-800x480
and I tested on 800x480 and 1280x720. 480x800 worked well, but 1280x720 follow 800x480 folder's dp and UI.
I don't know why this thing happens to me and I don't know how to solve the problem.
Why it doesn't work and what should I have to do?
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.
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
Thats code in the Manifest supports all dpis.
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
make sure you have given the corresponding drawables in drawable-xhdpi
I don't know on what basis you're creating the App. But it's better to avoid creating layout for specific screen sizes.. Instead you would give the following:
layout-small-port for devices like samsung galaxy y
layout-normal-port for devices like galaxy s2
layout-large-port for devices like nexus 7 which is 7 inch
layout-xlarge-port for devices having more than 7 inch size
If you specify the screen size, you can't support more number of devices
U should use:
res/layout/main_activity.xml # For handsets (smaller than 480dp available width)
res/layout-sw480dp/main_activity.xml # For phones (480dp wide and bigger)
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)
res/drawable – The default image folder
res/drawable-sw200dp
res/drawable-sw600dp
res/drawable-sw800dp

Different screen size layout [duplicate]

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?
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
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);
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
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.
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.

support for different screen sizes in android

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?
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
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);
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
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.
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.

support different screen sized

I have a project for an application that can run on different types of android device:
-small device :example samsung ace
-normal device:samsung galaxi s3
-tablet 10''
My layout is complex and the use of match_parent and "dp" isn't enough. I have read: http://developer.android.com/guide/practices/screens_support.html but I did not understand all of it.
Will I have to create more XML's in say, res/layout-sw720dp/main_activity.xml folder ow will I have to use different qualifiers? How exactly does it work?
For Different screen size, The following is a list of resource directories in an application that provides different layout designs for different screen sizes.
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
The following code in the Manifest supports all dpis.
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
Kindly Use This qualifier for your Android Project with compatible screen size.
Resource Screen Size
layout-small <3 inch
layout-normal 3-4 inch
layout-large >4 inch<7.1
layout-xlarge 7.1 - 10.1 inch
If you still facing problem use this Tutorial Link
If you want to create 1 application for different devices then you have to use android different folder as defined in the sdk
for e.g
drawable-ldpi - to place the images for the low screen density devices(240*320)
drawable-mdpi - to place the images for the middle screen density devices(320*480)
drawable-hdpi - to place the images for the high screen density devices(480*800)
drawable-xhdpi - to place the images for the extra high screen density devices (above 480*800)
If you want to create application for the tablet
drawble-sw720dp - to place the images for the tablet devices(7")
drawable used to place images. you have to create the layout for different drawable folder
layout-ldpi - to place the layout for the low screen density devices(240*320)
layout-mdpi - to place the layout for the middle screen density devices(320*480)
layout-hdpi - to place the layout for the high screen density devices(480*800)
layout-xhdpi - to place the layout for the extra high screen density devices (above 480*800)
android automatically get the images and layout from the application according to the device density. but for this you have to define
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
so that application develop for the multiple screen resolution devices.
To develop an application which supports every density and resolution, we should keep following points in mind:
(1) Put different sized images(with the same name) in 4 different folders in the ratio:
ldpi : mdpi : hdpi : xhdpi = 3 : 4 : 6 : 8
(2) The sizes commonly used for these resolutions are:
ldpi = 36 * 36 px mdpi = 48 * 48 px hdpi = 72 * 72 px xhdpi = 96 * 96 px
But you can use your desired sizes for the images as well.(Just try to follow the ratio in different sizes as well.) Amongst of all these images, the image which suits the device's density , will automatically be picked.
(3) Besides this, you can also get the density of the device programmatically and set the layout accordingly, like this:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
switch(displayMetrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
// layout for small sized devices.
break;
case DisplayMetrics.DENSITY_MEDIUM:
// layout for medium-sized devices.
break;
case DisplayMetrics.DENSITY_HIGH:
// layout for big-sized devices.
break;
}

Creating common layout structures in android?

I am developing an application which can be installed on any android device. Where should I put the following code in manifest file?
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"/>
Also, I have created three folders layout-normal, layout-small and layout-large. Uniformly i maintain LinearLayout in all my layout and different resolution of images in drawable-hdpi, drawable-mdpi, and drawable-ldpi.
The problem is layout is not suitable for some device, because it take different images based on density and i use mostly WRAP_CONTENT for most of component.
The alignment is not uniform in all device.
How can I maintain the Uniform alignment and common layout and images to support all android device Version?
If I am understanding you correctly... you want to display the proper layouts based on screen densities. Is this correct?
If so then you will want to formally name your layout directories. From Android: Supporting Multiple Screens
For example, 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 medium, high, and
extra high density screens.
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

Categories

Resources