layout management in android - android

I have created an app for all screen resolutions. So for that, according to the documentation I have created 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.
For example:
res/layout-normal/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
Now while running my app in different device what I notice that some of the devices with different screen resolution take the layout from the same resource directories i.e. layout-normal, and example of such devices are:
HVGA (320 x 480)
WQVGA 400 (240 x 400)
WVGA (480 x 800)
WXGA (720 x 1280)
Due to the use of layout from the same resource directories i.e. layout-normal it very difficult for me to manage the space between the UI for all devices as they take the same layout. Because if I manage the layout for HVGA then it doesn't look good in other, because of resolution.
So is there any way to solve this problem? Please help me to solve this out.

Dont prepare xml for each layouts. just make images for all (ldp, mdpi , hdpi) with same name and put in this different folder as per size.
ldpi : 240X320
mdpi : 320X480
hdpi : 480X800
And give permission in android Android Manifest file.
<supports-screens android:normalScreens="true"
android:anyDensity="true" android:largeScreens="true"
android:smallScreens="true" />
i am apply this way to make universal app in android.it will work fine, try like this way..

#hasMukh has pointed the right direction, but if you want the layouts for those 4 devices to be more precise. I suggest you to use the "layout-density-resolution" format for layout folder naming. As an example,
for HVGA(320x480) layout folder should be "layout-hdpi-480x320"
for WQVGA(240x400) layout folder should be "layout-ldpi-400x240"
for WVGA(480x800) layout folder should be "layout-mdpi-800x480"
for WXGA(720x1280) layout folder should be "layout-mdpi-1280x720"
This method is only good if you are planning to target specific few devices..and you can keep only one drawabale folder and use dp values for layouts.

i have the same problem, i used another approach.
get device height/width :
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels;
and check the device height/width according to that i used layout like
if(screenWidth == 320 || screenHeight== 480){
setContentView(R.layout.test_320_480);
}else if(screenWidth == 240 || screenHeight == 320){
setContentView(R.layout.test_240_320);
}else if(screenWidth == 480 || screenHeight == 800 || screenHeight == 854){
setContentView(R.layout.test_480_800);
}
use the same id for each controls in the layout for same screen.i hv put all layout in layout folder and it works for me.

Related

Android layout Support multiple Screens

I know this question has been answered many times but those answers are not helping me out. My app is looking perfect on 5inch screen but on 6 inch and more the alignment is different. I have added tag in manifest also...and also designed layout,layout-large,layout-normal also.
I want to know what is the best way to make the layout supports all screen without making 3 or 4 layouts for one screen.
To support multiple resolutions, you may use multiple layout files, but using a single layout file makes file maintenance and management easier. When you need to modify part of the layout for different resolutions, it is good to use dimens.xml, which makes it easy to modify.
This link may help you:
http://developer.samsung.com/technical-doc/view.do;jsessionid=4D620DFCC329BEC0DB60E73C6B185743?v=T000000126
sorry for answering instead of commenting but that is because of my low reputation, you can find this article helpful for you for the developers of google
https://developer.android.com/guide/practices/screens_support.html, Also this video
https://www.youtube.com/watch?v=GTd7QVv7lzg
There are different ways to approach your problem.
1. You can create different XML s for a same layout based on the screen sizes like normal, 7 inch and ,for large screens (i don't recommend this though )
A set of six generalized densities:
ldpi (low) ~120dpi
enter code here
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
read here(how to create multiple XMLS) --> https://developer.android.com/guide/practices/screens_support.html
2.Give sizes based on a ratio
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenHeight = size.y;
screenWidth = size.x;
now you have values for the screenHeight and screenWidth of that particular devise
now position layouts
eg: how to position a view as linear
topLinearLayout = (LinearLayout) inflateView.findViewById(R.id.top_len_one);
topLinearLayout.setGravity(LinearLayout.HORIZONTAL);
topLinearLayout.getLayoutParams().width = width;
topLinearLayout.getLayoutParams().height = 495 * height / 1920;
eg: how to position a view as relative
RelativeLayout.LayoutParams relativeLayoutForPassword = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutForPassword.setMargins(screenWidth * 240 / 1080, screenHeight * 960 / 1920, 20, 0);
editTextPassword.setLayoutParams(relativeLayoutForPassword);
// 1080 is my default wire frame's design width , 1920 is height (in pix)
so its like ,
i have an image which is 1920 in height and 1820 in width
create a layout which has 210 height in my default wire frame's design height and set resize it to my devise screen size --> 210/1920 * your devise height
this keeps the perfect ratio for any screen

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;
}

android layout smallest width (sw800dp) and different densities

How can I put different resources for different dpi on ICS with the same sw800dp smallest width?
Details: There are two tablets with ICS 4.0.4. First one has 1280x800 resolution and mdpi (160) density. Second one has 1920x1200 resolution and hdpi (240) density. So in terms of smallest width they both have the same sw800dp qualifier but different mdpi/hdpi density qualifiers.
I need to have different layouts and images for these two resolutions.
So I created two directories:
layout-sw800dp-mdpi
layout-sw800dp-hdpi
I thought that each device will choose its own directory according to the smallest width AND density.
BUT both of them take resources from the same sw800dp-hdpi folder!
I'm very confused and do not know how to separate resources for that two different resolutions.
Any help is really appreciated.
Thanks in advance.
You should use the same layout, located in /layout/sw800dp, and create /drawable-mdpi , /drawable-hdpi to put your custom drawables, the system will apply the correct ones to each device, using the same layout. Those devices should have similar size and aspect...
Use this to get the density:
float density = getBaseContext().getResources().getDisplayMetrics().density;
Screen height:
int h = 0;
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
h = (int) display.getHeight();
Screen width:
int w = 0;
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
w = (int) display.getWidth()
After that just select the correct resources.

Android Screen sizes

I need to know the screen sizes of android devices to support multiple screen sizes application.
Look at this table: http://developer.android.com/guide/practices/screens_support.html#testing
You can use the pie chart here to have an idea of relative screen size usage: http://developer.android.com/resources/dashboard/screens.html
For a list of screen sizes, resolutions and dpi values, take a look at: http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density
To calculate the real dpi value, check here: http://en.wikipedia.org/wiki/Pixel_density#Calculation_of_monitor_PPI
I don't think there's a comprehensive list of all existing screen sizes, since new devices are coming out all the time. Have you seen the page on Screen Sizes and Densities and the documentation on Supporting Multiple Screens?
Different screen sizes are as follows.
xlarge screens are at least 720dp 960dp
large screens are at least 480dp x 640dp
normal screens are at least 320dp x 470dp
small screens are at least 320dp x 426dp
If you are plan to make an application which support for multiple devices, also you have to crate different layout directories for put different layouts.
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
If you are plan to add different sizes of images, put them in following folders accordingly. Android OS will automatically take the most suitable image out of them.
res/drawable-ldpi/my_icon.png // bitmap for low density
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
Android supports multitude of screen sizes. There is no list of specific screen sizes. Only approximate ranges. Read more at "Supporting Multiple Screens".
LDPI MDPI HDPI
Please see this: http://developer.android.com/guide/practices/screens_support.html
then this: http://developer.android.com/resources/dashboard/screens.html
then this: http://developer.android.com/guide/topics/resources/providing-resources.html
In terms of supporting different screen sizes I would start by taking a look at the Screen Support Reference, might be able to solve your problem better. To see a list of specific sizes take a look at Table 2
Here it comes!
(240, 320)
(240, 400)
(320, 480)
(360, 640)
(480, 640)
(480, 800)
(480, 854)
(540, 960)
(600, 800)
(600, 1024)
(640, 960)
(720, 1280)
(768, 1280)
(768, 1024)
(800, 1280)
(1080, 1920)
(1200, 1920)
(1600, 2560)
fresh from http://en.wikipedia.org/wiki/Comparison_of_Android_devices 's html sources parsed with:
import re
s = ""
with open("sizes.html", "r") as src:
s = src.read()
res = re.findall('([0-9]+)\s*[×xX]\s*([0-9]+)', s)
sizes = set()
for match in res:
size_int = [int(match[0]), int(match[1])]
size = (min(size_int), max(size_int))
if size not in sizes:
sizes.add(size)
sorted_sizes = list(sizes)
sorted_sizes.sort(key=lambda sz: sz[0])
for sz in sorted_sizes:
print(sz)
(forgive my python)
Here's a little function to know the inch size of your device in case you need it to support multisize screen :
public double getInchSize()
{
DisplayMetrics metrics = getResources().getDisplayMetrics();
return Math.hypot(metrics.widthPixels/metrics.xdpi, metrics.heightPixels/metrics.ydpi)
}
Hope it can help

How to differentiate layout for 480 * 800 and 480 * 854 screen resolutions in android?

In one of the application I need to make sure that UI components will be placed at proper position in all the screen resolution devices. I have gone through the support multiple screen resolutions tutorial on android developer site. Based on that it seems I may have to create separate layout files for small, normal and large screen devices. Now, the issue in this is that even in large screens there are different resolutions such as 480 * 800 and 480 * 854. In the screen the components gets misplaced slightly. I have set top margin as 100 dip then for 480 * 800 it appears properly but for 480 * 854 it is misplaced slightly.
Can someone let me know how to handle this now?
both the resolutions are considered under the layout-long, so you have to set the layout as per the device's height and width manually.
As per my view this is the best solution . I applied the same solution for my application .
Example
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
height = dm.heightPixels;
width = dm.widthPixels;
if (height == 854 || width == 480)
{
// Your view
}
else
{
// Your VIew
}
You have to check the above condition in onCreate() method of acivity..
It should be possible to differ between WVGA854 and WVGA800 by using:
res/drawable-hdpi-long/
res/drawable-hdpi-notlong/
but i would definately recommend you to design the layouts so that it is robust enough to use one set of layouts for those two screens. It will be too much work to maintain/test of you can't design one layout in that case.

Categories

Resources