How to load different drawable resource for different layouts in Android? - android

In my application,I have tow alternative layout files and two alternative drawable resource images. My res directory structure is as follows:
res/drawable-hdpi/image.png
res/drawable-mdpi/image.png
res/layout-large/main.xml
res/layout/main.xml
in manifest file
<supports-screens
android:resizeable="true"
android:smallScreens="true"
android:largeScreens="true"
android:anyDensity="false"/>
My app loads both the layout files res/layout-large/main.xml and res/layout/main.xml but, both the layouts load image from res/drawable-mdpi/image.png and does not load res/drawable-hdpi/image.png. Please help me in this respect I would be very thankful to you for this act of kindness. Thanks in advance.

I think what you are looking for is to use configuration qualifiers.
It seems you are really misunderstanding what these folders do.
Your android will select folder based on it's screen size or pixel-density of device on which u run it
If Your device has a medium Pixel density and a large screen. So it selects its resources from the res folders with those given qualifiers.
res/layout-large/my_layout.xml
and images from
res/drawable-mdpi/my_icon.png
You cannot tell your device to get images from the hdpi folder because it does not have a high pixel density.
So you could either create a folder called
res/drawable-large-mdpi/
specifically for your device.
Or just make sure the right images are in the right folders.

You can create drawable-large-mdpi folder to use the drawables for your layouts which are under layout-large folder.

Related

Android scale image for XXHDPI

How can I make 2 separate layout folder for
screens that are xxhdpi, and screens that are everything else?
Thanks!
I have right now just a layout folder for all my layouts
You can have folders under /res that are named as follows (where "xxhdpi" refers to the Density):
layout
layout-xxhdpi
However, you may actually be referring to the Size of the screen itself, which can be:
layout
layout-xlarge
as an example...
Please refer to the Android Providing Resources docs for more information on the flavors of folder names for resources.

Screen Resolution for different Android Devices [duplicate]

This question already has answers here:
Application Skeleton to support multiple screens
(2 answers)
Closed 9 years ago.
How to Solve Screen Resolution Problem?
I want my application run on any android device, and its layout should not change,
I read android Blog of Supporting Multiple Screens,But do not get enough idea.
I have created different layout folder like layout-small,layout-normal,layout-large,layout-xlarge and created different XML files for all folder.
But when i run on Tablet it takes Default normal size XML file,I don't know why?
Please help me to solve it, Thanks in advance and Waiting for response
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
Add this to your manifest file of app.
Create the layout and drawable with following qualifiers
layout-sw600dp drawable-sw600dp
which means tablet screen with minimum of 600dp and higher can use this layout and drawable.
So for standard configuration qualifiers for screen size you can create following configuration:
1)layout-sw480dp drawable-sw480dp
2)layout-sw600dp drawable-sw600dp
3)layout-sw720dp drawable-sw720dp
check http://android-developers.blogspot.in/2011_07_01_archive.html
Following the guide will only help you to deal the problem for the most cases, but never all the scenarios. Check the following list which is the Google's AOSP homescreen source code. Even this cannot guarantee it will work for all the devices. So yours are far from enough.
drawable
drawable-hdpi
drawable-land-hdpi
drawable-land-mdpi
drawable-land-xhdpi
drawable-mdpi
drawable-nodpi
drawable-sw600dp-hdpi
drawable-sw600dp-land-hdpi
drawable-sw600dp-land-mdpi
drawable-sw600dp-mdpi
drawable-sw600dp-nodpi
drawable-sw600dp-xhdpi
drawable-sw720dp-hdpi
drawable-sw720dp-land-hdpi
drawable-sw720dp-land-mdpi
drawable-sw720dp-mdpi
drawable-sw720dp-nodpi
drawable-sw720dp-xhdpi
drawable-xhdpi
layout
layout-land
layout-port
layout-sw600dp-port
layout-sw720dp
layout-sw720dp-port
According to this page the layout-small,layout-normal,layout-large,layout-xlarge
are deprecated and you will use the
36x36 for low-density
48x48 for medium-density
72x72 for high-density
96x96 for extra high-density
as alernative, however for further information please check my answer here

Android XML: Why can't we type #drawable-mdpi, and instead, we must type #drawable?

I see the folders:
drawable-hdpi
drawable-mdpi
drawable-ldpi
I have seen countless threads of "Type #drawable/some_image.png" and "Create a drawable folder in res."
But never did I see a question about XML referring to any drawable folders with qualifier names. Or basically writing XML to refer to any folders with qualifier names.
Why is this not documented? Why XML can't refer supported resource directory names with qualifiers? Is it because of the limitation of XML when it comes to hyphens?
I'm just a newbie. I'm just asking. No big deal about it, I'm just being curious. Thanks for answering.
It is done that way to support multiple screen sizes/resolutions.
You put the same image (with the same name) in the various folders, modified to look best for that resolution (usually different pixel densities). When there is a call for that image name, the system then goes through those folders and picks the best image (of those you've supplied) for the screen resolution for the device that is being used.
Because resources in hdpi, mdpi, ldpi will use the same Resource ID.
The os determine the device which resource will be use.

Android generic application requirement

I want to develop an Android application which can run on all ,screen type ,Android phones. So my question is:
Would it be okay to to keep all image resources in HDPI folder only? Or will I need all 3 type of resources LDPI , MDPI AND HDPI?
If I use relative layout for developing this app so what would happen for the images which have some text on it ( _ button background image which will have some text "Submit" ). Android will scale the image according to device screen but is it capable to scale text also accordingly?
Please suggest me how to deal with this problem.
How to make an app which can work on all type screen Android phones?
To develop a android application that has mutiple screen support you must go through this tutorial in brief:Android Mutiple Screen Support
In gist of your questions:
1.The specific asset in drawable are automatically taken depending on density.
So you should place specific images in mdpi,hdpi,ldpi accordingly.
2.Read and understand manifest support screen tag values:
<supports-screens android:smallScreens="true"
android:resizeable="true" android:largeScreens="true"
android:anyDensity="true" android:normalScreens="true"></supports-screens>
3.The Text should be resized:
You should use styles, then you can have separate folders "values" (default) "values-hdpi" (high density) "values-mdpi" (medium density) and so on and put your style file with correct textSize values in each folder as needed.
Then, when you are in medium density device it will pick the file in "values-mdpi" folder if exists or in "values" if not, and the same for high density etc...
This same principle applies to al "res" subfolders (drawables, values, etc...)
Or Simply If do not go for styles just use dp instead of sp for text dimension.
If you put images in HDPI folder only, it will work. but ideally you should put images according to density. also if you use 9 patches image on button background, android stretches the images according to requirement however if in some case you find that image are not properly stretching you can use separate images for landscape and portrait mode.
If I use relative layout for developing this app so what would happen for the images which have some text on it ( e.g a button background image which will have some text "Submit" ) . android will scale the image according to device screen but is it capable to scale text also accordingly?
=> If you want to provide scalable images which can be shrinked/stretched automatically as per the screen size, then there is a 9-patch tool by which you can create a scalable image and after that you don't need to worry about the screen size and screen density.
Here is a nice example: http://www.dibbus.com/2011/03/9patch-images-in-android/
Here is also a nice article and guide to create 9-patch image: http://radleymarx.com/blog/simple-guide-to-9-patch/

How can I alias an android bitmap to a drawable from another size (drawable-large-mdpi aliases to drawable-hdpi)

I'm working on adding support for tablet sized screens to my apps. I already have images in drawable-mdpi and drawable-hdpi for different density screens. My problem is with tablets like the Galaxy 7" which is a "large" screen but is still medium density. Part of my layout has 5 buttons across the width of the screen which are evenly spaced. On the large screen with mdpi graphics though the images are very small with tons of whitespace between them.
I would like to use larger graphics for the large layout to make them look appropriate as well as take advantage of the screen real estate. I have some double sized graphics in my hdpi directory that would work perfectly. As a test, I've copied all of the images from /res/drawable-hdpi into /res/drawable-large-mdpi and everything looked exactly as I want.
However, I don't want to bloat the size of my app by coping all of those images. I would like to just create aliases for each of them so that when /res/drawable-large-mdpi/button_0 is requested, it will actually use /res/drawable-hdpi/button_0 instead.
I've tried creating an xml bitmap but I don't know how to reference a drawable from a specific directory. Any help?
Contents of /res/drawable-large-mdpi/button_0.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable-hdpi/button_0" />
The error I get with the above is:
error: Error: No resource found that matches the given name (at 'src' with value '#drawable-hdpi/button_0_highlighted').
Move your button resource into the drawable-nodpi folder and rename it to button_0_hdpi.xml.
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/button_0" />
When using an XML alias you can not specify the qualifier. You had #drawable-hdpi and it needs to be just #drawable. You probably also need to make a second XML alias in the original location of you button bitmap.
Here is a good article on the method http://blog.evendanan.net/2011/03/Android-resources-and-device-fragmentation
this page talks about the different modifiers you can use on resource folders. It seems to indicate that the order of precedence is such that screen size(small, med, large, xlarge) is higher than density(ldpi, mdpi, hdpi). I would think that this means if you renamed your drawables-hdpi folder to drawables-large-hdpi even though the Galaxy tab has a medium density it will still use the drawables from this folder because it has a large screen.
Edit: I just tested this out, it does solve your problem one way. They images inside the drawables-large-hdpi folder to show up on the Galaxy tab when running the app. But unfortunately adding the large qualifier makes it so they don't show up on medium sized screens with hdpi densities. Its looking like you might have to make separate folders and have 2 copies of your large resources if you want to get this functionality =(.

Categories

Resources