Resources for different tablets android - android

I need to put different resources for two Samsung tablets in my application.
600x1024 (Samsung Galaxy Tab 7in)
800x1280 (Samsung Galaxy Tab 8.9 in)
So i created two different folders drawable-port(for 7 in) & drawable-large-port(for 8.9in)in res folder but both the devices are picking the resources from drawable-large-port folder.
when i run getResources().getConfiguration().screenLayout for both the devices output comes out to be
SCREENLAYOUT_SIZE_LARGE
so what should be the folder names so that both the devices pick the resources from different folders.
Thanks
Narinder

The two devices are both LARGE screens, so in this case they are both picking up the same resource when they look in your drawable folders. They will always go to drawable-large-port for portrait and drawable-large-land for landscape.
You can add to this functionality by creating different layouts for the two devices, that use the drawables in drawable-large-port in different ways to take advantage of the different size of screen.
You can define different sizes as below for your layouts:
res/layout-sw600dp/ // For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/ // For 10” tablets (720dp wide and bigger)
You can read more about dealing with different sized screens/devices at http://developer.android.com/guide/practices/screens_support.html

You can measure the width of screen using:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
System.out.println("width :: "+width);
System.out.println("height :: "+height);
Then you can set Drawable, using width as:
if(width == 600)
// set drawable-1
else if(width == 800)
// set drawable-2

The better result put the following in the drawables
---------------- for Phones ------------------------
drawable-ldpi
drawable-mdpi
drawable-hdpi
----------------- for 7 inch tablets ----------------
drawable-large-mdpi
drawable-large-hdpi(for Nexus 7)
---------------- for 10 inch tablets ----------------
drawable-xlarge-mdpi

Related

How to make my app look the same in all devices?

I'm newly on android development and i build an app on android studio and i want to make my app have same design in all devices:
Samsung Galaxy S4 => 1080x1290 ; 5.0”
Galaxy Nexus => 720x1280 ; 4.7”
Nexus 4 => 768x1280 ; 4.7”
Motorola Droid Razr M => 540x960 ; 4.3”
Nexus S => 480x800 ; 4”
Galaxy S2 => 480x800 ; 4.3”
Galaxy Ace => 320x480 ; 3.5”
Galaxy Note => 800x1280 ; 5.3”
Galaxy Note II => 720x1280 ; 5.5”
Nexus 10 => 2560 x 1600 ; 10.1”
Galaxy Tab 10.1 => 1280*800 ; 10.1”
Galaxy Note 8.0 => 1280*800 ; 8.0”
Galaxy Tab 7.7 => 1280*800 ; 7.7”
Nexus 7 => 1280*800 ; 7.0”
Galaxy Tab => 1024*600 ; 7.0”
and i read these and lots of question here
http://developer.android.com/training/multiscreen/index.html
http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts
i used the width/height in "dp" and made this with all items
android:layout_margin
android:paddingLeft
android:paddingRight
android:paddingTop
android:paddingBottom
and it works fine, but as i know there is a method that read the device screen then decide which layout will open in the app, assume i have one layout and i have these size for it
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)
My question is : how i code my java activity to read the device screen which my app run on it then decide which layout will open like if the device is S2 then the java make something like this:
if (tabletDeviceSize) {
//use tablet support layout
} else
{
//another layout for mobile support
}
i see this code, but i need the complete one because i'm not perfect in coding :(
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
break;
case DisplayMetrics.DENSITY_MEDIUM:
break;
case DisplayMetrics.DENSITY_HIGH:
break;
}
According to the official documentation, you don't need to programmatically decide which layout to use with the respective screen size.
To optimize your application's UI for the different screen sizes and
densities, you can provide alternative resources for any of the
generalized sizes and densities. Typically, you should provide
alternative layouts for some of the different screen sizes and
alternative bitmap images for different screen densities. At runtime,
the system uses the appropriate resources for your application, based
on the generalized size or density of the current device screen.
In other words, if you follow the recommendation stated in the documentation, as I can see that you've done, placing your layout files in their respective resource folder like so:
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)
Then the system will decide, which layout to use. No additional code is needed for you to specify it at run time.
If you however would want to make changes depending on your screen resolution, you could get the width and height in pixels using the following code
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Then do something cleaver depending on the width and height variables, e.g. in your case with the S2:
if(width == 480 && height == 800){
//Do work that's related to the S2 screen resolution
}else if(...){
}
If you're asking how to get the density of the device's screen, it took me 3 seconds to find that answer (not including the time to type "android get device density" into my favorite search engine):
getting the screen density programmatically in android?

Layout for 720x1280 phone

I am using the emulator to test the screen layout for Motorola Razr phone at Landscape mode.
The phone dimensions are not common like other phones and so I am getting smaller layout with respect to the screen size of the phone.
Physical size: 4.5 inches
Resolution: 720 x 1280 pixels
Pixel density: 326 ppi
I already have folders for handling multiple size tablets and phones in res folder -
layout
layout-large
layout-large-land
layout-large-land-tvdpi
layout-sw600dp-land
layout-xlarge
So the phone (Motorola Razr) is using layout folder xml to display the contents but this is small enough for the phone resolution.
What should be the layout folder for this phone specification ?
If I create layout-sw720dp folder and put the layout file there, the application still takes layout file from layout.
If I create layout-w720-h1280 folder then I gets compile time error after creation with red cross mark on this folder.
The post new-tools-for-managing-screen-sizes mentions -
Typical numbers for screen width dp are:
320: a phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
480: a tweener tablet like the Streak (480x800 mdpi).
600: a 7” tablet (600x1024).
720: a 10” tablet (720x1280, 800x1280, etc).
But then my device is not a 10” tablet but a 4.5 inch phone so that to use the smallest dp value.
UPDATE:
The Motorola Razr phone is a xhdpi device, so I have to divide a pixel-based dimension by 2 to get the dimension in dp. So the Motorola Razr is 640dp tall, and 360dp wide.
layout-sw720dp - Wrong folder.
layout-sw360dp - Right folder.
With the above statement in account I created a layout-sw360dp folder and kept my layout files there. Now the application is successfully taking the layout files from this folder to show for this device.
But now, another problem arises. My 7' tablet device is also taking layout files from this folder instead of layout-large-land from where it was taking the layout files previously.
So again this idea dropped!
Any help....

Android drawable resources issue

I've noticed that real device picks up different resource from that eclipse shows in preview.
For example I created drawable resources for devices with smallest width of 720dp as
drawable-sw720dp-mdpi for Samsung Galaxy Tab 1280x800 mdpi
drawable-sw720dp-hdpi for Nexus 10 2560x1600 hdpi.
But Samsung Galaxy Tab 1280x800 mdpi picks up the drawable-sw720dp-hdpi instead of drawable-sw720dp-mdpi ?
Same thing with other devices. If there are resources drawable-sw320dp-hdpi, drawable-sw320dp-xhdpi, the Sony Ericsson xperia-ARC 854x480 hdpi picks up drawable-sw320dp-xhdpi ?
I cant just keep drawable-hdpi resource because there are devices 5'5 inch screen size. For example samsung galaxy 1280x720 hdpi 5'5 inch, and for such devices there is different graphical design so both devices 854x480 hdpi and 1280x720 hdpi will be using the same directory drawable-hdpi wich is not acceptable.
Any ideas?
Some time ago I had similar problem, while writing animated wallpaper which was using canvas and bitmaps. I couldn't achieve what I wanted using default layout folders.
In my case, for example, medium density device with xlarge screen (1280x800), should use the same resources as high density with normal screen (600x1024).
I've decided to write my own solution, by putting resources into three groups and selecting them using custom rules.
DisplayMetrics metrics = resources.getDisplayMetrics();
int screenSize = resources.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
switch (metrics.densityDpi) {
case DisplayMetrics.DENSITY_LOW:
if (screenSize == Configuration.SCREENLAYOUT_SIZE_SMALL || screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
return new SmallResourceProvider(resources);
}
case DisplayMetrics.DENSITY_MEDIUM:
if (screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
return new MediumResourceProvider(resources);
}
default:
return new LargeResourceProvider(resources);
}
Maybe this will be helpful in your case.
I was also facing the same strange issue. In my case Galaxy tab GT P7500 was picking images from drawable-sw720dp-XXHDPI folder as i had the XXHDPI folder also. If i deleted the XXHDPI, it picks up the next lower which is the XHDPI folder.
The issue is with the swxxxdp qualifier for drawable folders. If i remove the screen width and use just 1 set of images of all densities the issue seems to be resolved. However i wanted to have different set of drawables for 7 inch and 10 inch devices. What i did was use a different set of names for the 10 inch images and 7 inch images and use 2 different set of layouts with sw600dp and sw720dp qualifiers.
Here is my current structure which works well. However this is not the expected way of using the resources, as android recommends externalizing the drawables. unfortunately i couldn't find a better solution.
layout-land
layout-sw600dp-land (uses image.png)
layout-sw720dp-land (image_large.png)
and for drawables,
drawble-mdpi (image.png & image_large.png)
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi

How to set layout on 7" two different tablet?

I have two 7 inch tablet one tablet (800*480) and second tablet (1024*600). I have facing problem following :-
1. Both can run on layout-mdpi and layout-large so how can i make different folder for run my app both resolution.
Application run xml for mdpi layout in both tablet:-
layout-mdpi
layout-hdpi
Application run xml for large layout in both tablet:-
layout-large
layout-xlarge
I can implement http://developer.android.com/guide/practices/screens_support.html this but not use full to me.
or
try so many link but still same problem both tablet running in same layout.
Try using this.It worked for me
layout-sw600dp
7" tablets—Android 3.2 introduces a new way to specify resources for more discrete screen sizes. The new technique is based on the amount of space your layout needs (such as 600dp of width), rather than trying to make your layout fit the generalized size groups (such as large or xlarge).
res/layout-sw600dp/
res/layout-sw600dp-land
res/layout-sw600dp-port
320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800
hdpi, etc).
480dp: a tweener tablet like the Streak (480x800 mdpi).
600dp: a 7” tablet (600x1024 mdpi).
720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).
UI to differentiate between sizes such as 7” and 10” tablets
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)
xlarge is a configuration qualifier for extra large screens.* When you append this string to a resource directory name (such as layout-xlarge), it indicates to the system that these resources are to be used on devices that have an extra large screen.
I have two 7 inch tablet one tablet (800*480) and second tablet
(1024*600). I have facing problem following :-
I have faced the Same issue. as a workaround i have make the xml file for (800*480) in same layout folder as Default.
Also used layout-sw320dp for the Devices like GalaxyNexus
and for 7'' Tablet i have make layout-sw600dp for 7'' Screen Tablet
and for 10'' Tablet i have make layout-sw700dp for 10'' Screen Tablet
For Example : you have the xml file named "activity_main.xml"
1) inside layout folder--> put activity_main.xml with layout according
to nexus one(480*800 hdpi)
2) inside layout-sw320dp folder--> put activity_main_tab.xml with
Layout according to Galaxy Nexus(720*1280 xhdpi) Device which
should take the layout from layout-600dp but will take layout from
layout-320dp
3) inside layout-600dp folder--> put activity_main_tab.xml with layout according
to tablet 7'' Screen
4) inside layout-700dp folder--> put activity_main_tab.xml with layout according
to tablet 10'' Screen
after making the Layout formated as above. i have checked runtime width and height of device. and set the layout file accordingly.
if (displayWidth >= 552 && displayHeight >= 976 || displayWidth >= 976
&& displayHeight >= 552) {
Log.i(TAG, "in tab xml");
setContentView(R.layout.activity_main_tab);
}else{
Log.i(TAG, "in Simple xml");
setContentView(R.layout.activity_main);
}
Hope this will Help.

How to include 10" and 7" layouts properly

Nexus 7: 7" 1280x800
Galaxy tab 10.1 10" 1280x800
I want my app to run on 7 and 10 inch tablets. As far as I know, I have to include these layout folders in my app:
for 7 inch tablets
layout-sw600dp
layout-sw600dp-port
for 10 inch tablets
layout-sw720dp
layout-sw720dp-port
It runs fine on the nexus 7, but loads the sw600dp layouts on the 10" tablet.
If I include these default folders:
layout
layout-port
10" galaxy tab loads layouts from these.
If I only include the default layout folders and the sw600dp one, it crashes on the nexus7.
How am I supposed to support phones, 7" tablets and 10" tablets, if the 10" galaxy tab won't load the sw720p layouts?
edit:formatting
The problem was, that I had no default layout folder.
I tried getting by, using only the sw600dp and sw720dp folders. I still have no idea why they don't work, but I don't care. I can't use swxxxdp <3.2 anyway, so screw that.
So if you want to write an app, that has to support phones(2.2+), 7inch tablets and 10 inch tablets, use the following oldschool stuff:
layout this is the default, it is needed even if you don't plan to support phones!
layout-large for 7" tablet (works on emulator and nexus7)
layout-xlarge for 10" tablet (works on emulator and galaxytab10.1)
Other people have came to the same conclusion too.
I am also facing such problem in my application. But I found a good solution for this.
I have only one layout for tablet and directory name is layout-sw600dp.
Now, when part came to height and width problems, I have created several different values directory in which i place dimensions and font size and other stubs. So there will be no constant value in layout of tablet screen.
androd:layout_width:"60dp" // i drop this scenario
androd:layout_width:"#dimen/tab_width" // i used this scenario
and your values directory name will be like
values-xlarge
values-large
All the values will be fetched from your values directory. It will not create different layout, but one layout can be used multiple times.
Following are words of Developer.android site.
Configuration examples
To help you target some of your designs for different types of devices, here are some numbers for typical screen widths:
320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
480dp: a tweener tablet like the Streak (480x800 mdpi).
600dp: a 7” tablet (600x1024 mdpi).
720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).
Using the size qualifiers from table 2, your application can switch between your different layout resources for handsets and tablets using any number you want for width and/or height. For example, if 600dp is the smallest available width supported by your tablet layout, you can provide these two sets of layouts:
res/layout/main_activity.xml # For handsets
res/layout-sw600dp/main_activity.xml # For tablets
===
In this, you can see that, layout for 1280*720 is under layout-sw720dp so instead of creating layout-normal-xlarge you should use this thing which lets you to decide differences. Instead of identify differently using layout-large-mdpi and layout-large-ldpi, are't you just identify by its smallest width? Because, android providing drawables directory for different images, only thing is its resolution. And you have above solution.
Edit
Then you must have to develop different layouts. No other option. I found at http://jamil.fluidsoul.net/2011/03/06/creating-android-applications-for-multiple-screen-sizes.
Low density Small screens QVGA 240x320 (120dpi):
layout-small-ldpi (240x320)
layout-small-land-ldpi (320x240)
Low density Normal screens WVGA400 240x400 (x432) (120dpi):
layout-ldpi (240 x 400 )
layout-land-ldpi (400 x 240 )
Medium density Normal screens HVGA 320x480 (160dpi):
layout-mdpi (320 x 480 )
layout-land-mdpi (480 x 320 )
Medium density Large screens HVGA 320x480 (160dpi):
layout-large-mdpi (320 x 480 )
layout-large-land-mdpi (480 x 320)
Galaxy Tab ( 240 dpi ):
layout-large (600 x 1024)
layout-large-land (1024 x 600)
High density Normal screens WVGA800 480x800 (x854) (240 dpi):
layout-hdpi (480 x 800)
layout-land-hdpi (800 x 480)
Xoom (medium density large but 1280x800 res) (160 dpi):
layout-xlarge (800 x 1280)
layout-xlarge-land (1280 x 800)
Yes, you should use layout-dependent folders but also make sure any device independent layouts go in your res/layout folder.
This is mentioned on the Android developer site but to reiterate their point, if you have a layout that is only available in an layout-xlarge folder on an app that supports say large and normal sized devices as well, the app will crash as smaller devices will not be able to find any match for this resource.
Here is a good folder structure, start with:
res/layout
Keep a device-independent layout in there, you can avoid this if you are accounting for every possible qualifier type but this is still the safer option.
If you want to add specific layouts for say 7 and 10 inch tablets, use all of the following:
res/layout-large
res/layout-xlarge
res/layout-sw600dp
res/layout-sw720dp
res/layout-sw800dp
And so on for any specific device screen widths you want to support. It should be noted that sw600 supports the smallest possible width, so avoids the use of the screen width when the device is held landscape. Using the swxxxdp qualifers are preferred but these were added in API 13 so you will still need large, xlarge for older Android OS.
Regarding use of the dpi, be aware that if you ONLY set one density qualifier for a size, so layout-large-mdpi for example, then any devices that match the large qualifier will use layouts from here instead of another folder, this is due to the Best Match criteria, which you can read about here. It will match it as a large device before it will match the density so non-mdpi density screens will still use these layouts.
To counter this, you will have to include folders for whatever other densities you support as well, so layout-large-hdpi following on from the above example, and include in this folder hdpi versions of layouts that you have used in the mdpi folder if you require them to be different.
Avoid duplicating your layouts as well of course, don't copy device-independent layouts into every unused folder if you only need them in res/layout, try and only keep the layouts that need these qualifiers in the folders and organise them properly, making sure your folders are named with the qualifiers in the right order of precedence to prevent using the wrong folder for a density/size combination.
This is very strange, since you are doing the correct thing.
The sw600dp qualifier should be selected by the Nexus 7.
If available, the sw720dp qualifier should be selected by the Galaxy Tab.
Are you sure it crashes when trying to find an appropriate layout? Android may find the correct layout, but something in the layout xml file may be missing and the crash is caused by that.

Categories

Resources