I am wondering how to get different XML layout files to be read depending of density of the screen.
Now when I enter a layout file and set to so that it has a couple of buttons and have them set to a specific position, the position changes when I change to another screen density in the emulator. This is what should be happening what i understand- but How do I get the program to use different layouts depending of densities?
I have been reading on android dev. page on how to develop for different screens, but I didn't get the examples which were there.
I have been using dp/dip on sizes so no need to suggest that =)
you can do it by creating a layout for each standart screen sizes,
you can find here how to do it.
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
it means you should create sub-directory named as ("layout", "layout-small","layout-large","layout-xlarge","layout-xlarge-land") in res folder.
than create a main xml for each one with the same name.
By placing the layout XML files in different folders you can get Android to load the correct one depending on the screen density. For example, if you layout is called "main.xml":
Placing the file in /res/layout-ldpi/main.xml will mean it is used only in low density (or above)
Placing the file in /res/layout-mdpi/main.xml will mean it is used only in medium density (or above)
Placing the file in /res/layout-hdpi/main.xml will mean it is used only in high density (or above)
Placing the file in /res/layout-xhdpi/main.xml will mean it is used only in extra-high density
You can do the same sort of thing with all resources- drawables, strings, dimensions etc. (e.g. drawable-mdpi, or values-en [values only to be used in an English locale]). You can find the whole list of different device set-ups supported by this system at http://developer.android.com/guide/topics/resources/providing-resources.html (Table 2: Configuration Qualifier names)
Related
i've finished my layout for medium screen (5 inch i think), but i've trouble for adapting my drawable, button, and text for large and xlarge screen. How can i adapt them ? Please tell me step by step, because i feel very difficult for following many resource that i've got from many different resource and person. Thank you.
Firstly i strongly recommended you to go through the android developer docs for How to support multiple screen in Android and Supporting Different Screen Sizes
Coming to your question, 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
You can use the following resource folders to create layouts for devices with larger screens : (for api level greater than 3.1 - mentioned here)
layout-sw320dp
layout-sw480dp
layout-sw600dp
layout-sw720dp
Your resource structure will be like this in your project
Coming to the conclusion of my answer, For supporting the multiple screen support there are different different ways is there, you need to find the better one among them.
Here is some approaches what you can try it !!
Instead of different layout folders try to create a different values folders in your project and place the dimens.xml folders in it and place all your dimension values there. here is my answer
Create the layout folder like mentioned in above answer and keep the
respective xml files in such folders. It automatically takes from
the respective folders for respective devices( ex; it will load the
xml resources form layout-sw<600> folder for a 7'inch tablet) here you can get this approach
Programatically differentiate your layout files for different set of
devices (bit of difficult approach,but works) here is link
You have to change the size of the image for different screen sizes. Android takes the image automatically depending on screen size.
The size of image for
ldpi is 36px * 36px
mdpi is 48px * 48px
hdpi is 72px * 72px
xhdpi is 96px * 96px
xxhdpi is 144px * 144px
You can get different sized images using online tools. Place proper images in different folders it will work fine.
Check this link for more information:
http://developer.android.com/guide/practices/screens_support.html
I that When starting an android application, android makes a good "guess" as to which layout/drawable to use depending on the screen size. However, I was wondering where it chooses this from? I ask this because I have quite a few layouts which don't all work. I read on the android website that you can make a folder called layout-xhdpi, layout-ldpi... etc. But does android automatically look for layout-xhdpi or layout-ldpi or do I need to specify that anywhere?
Same goes for drawables. When creating a new application in Eclispe it makes automatically drawables folders for you but does this necessarily mean it looks for those.
Conclusion:
Where does android look for alternative screen sizes and how/where can I change that?
Create following layout folder
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
I implemented all layout files for hdpi devices. And put them in "layout" forder. Now I tested on a normal-screen device, and there's some screen I urgently need to change, because a required input field doesn't appear (because of the small screen).
What I did is create a folder called "layout-normal" and put there a copy of my layout file, and reduce in the copy everything a bit so it fits the screen. Now I have 2 problems and don't understand anything.
Both files point to the same source - when I open them I don't get 2 files, but only 1 with my modifications.
Ran this on the normal-screen device and it still looks like at the beginning.
Please help :/
layout-normal and just layout are the same. It is just the default layout
Ref: android developers site
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
..
Size small Resources for small size screens.
normal Resources for normal size screens. (This is the baseline size.)
large Resources for large size screens.
xlarge Resources for extra large size screens.
it is possible to build layout (ex: menu)
that will use coordinates system pt
I need to build menu for game and I need to set all fields on same positions on every screen size
if screen will be big it must be scale everything on the layout and same on small screen size
like this:
can you provide some example for it?
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
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
This has been taken from http://developer.android.com/guide/practices/screens_support.html
If you sort your xml files this way, Android itself figures out what file to choose depending on the screen size of the device.
While designing the desired layout, you can opt for Relative Layout which usually sticks to its attributes such as alignParentBottom, alignParentTop, alignParentRight, alignParentLeft to name a few.
I hope this solves your issue, although I'm not clear what you're talking about when you say "coordinates system pt"
create the image with 9-patch so no need to save the images for different density like hdpi,mdpi etc. it will adjust with control as you define the size or you can save images with different size into the hdpi, mpdi with same name and by runtime in device it will get the appropriate images as per the density available
edit
links for 9-patch
http://developer.android.com/guide/developing/tools/draw9patch.html
http://android10.org/index.php/articlesother/279-draw-9-patch-tutorial
http://www.androiddom.com/2011/05/android-9-patch-image-tutorial.html
I'm looking to make an Android app that changes the look of the UI based on the device it's shown on (4 inch phone, 7inch tablet and 10inch tablet). I know you have the fragments API and it's probably that which I should use for this, but all the examples I've seen just have 2 activities sitting side by side and the code is a bit OTT for my needs (they have events passing between them etc).
Here is the comparison between a 4 inch device and a 7 inch device and how the different UI elements should move around (also note that element A can change design between the two devices).
Any ideas on how I would go about doing this, or if anyone has any sample code that would be fantastic!
I think you should probably design different xml for each size. You can use the qualifier in the layout dir for different screen size. http://developer.android.com/guide/topics/resources/providing-resources.html#AliasResources
You can use different directory for different size :
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
and for more information :
http://developer.android.com/guide/practices/screens_support.html
Good Luck,,, :D
See also Fragments for Android 3.0+.