Do I need style file in each DPI folder? - android

If I have only one style file in values folder and it contains items refer to items from dimens file then I don't get right result. Only if I will have style file in each values folder (MDPI, HDPI, etc) I will get right result.
That is a bit strange because for drawable I could have files only in one folder and resources for different DPI in other folders and it works fine.
Could someone explain how Android search style items when I use references from dimens?
Here is a little example.
values/styles.xml
<resources>
<style name="message_item_topic">
<item name="android:textSize">#dimen/message_id_topic</item>
</style>
</resources>
values/dimens.xml
<resources>
<dimen name="message_id_topic">12sp</dimen>
</resources>
values-hdpi/dimens.xml
<resources>
<dimen name="message_id_topic">15sp</dimen>
</resources>

As per the Providing Alternate Resources guide:
Android supports several configuration qualifiers and you can add multiple qualifiers to one directory name, by separating each qualifier with a dash. Table 2 lists the valid configuration qualifiers, in order of precedence—if you use multiple qualifiers for a resource directory, you must add them to the directory name in the order they are listed in the table.
The rules for finding the best matching resource:
Eliminate resource files that contradict the device configuration. (Exception: DPI)
Pick the (next) highest-precedence qualifier in the list
Do any of the resource directories include this qualifier?
If No, return to step 2 and look at the next qualifier. (In the example, the answer is "no" until the language qualifier is reached.)
If Yes, continue to step 4.
Eliminate resource directories that do not include this qualifier
As per the flowchart:
Each resource is loaded according to these rules separately (i.e., a lookup for each dimen happens for each, independent of the other resources). Any display issues you are having are probably due to not knowing the difference between things that scale by DPI (dp and sp) and things that do not (px) - use dp and sp and you do not need to declare alternate resources (for anything but drawables) for different DPI devices.

If you were using px, cm, in, ... then a different dimens.xml in separate values-(l|m|h|xh|xxh)dpi would make sense.
Things are relative.
And there are too many devices around.
You'll never be sure your app will fit on EVERY existing device.
Some users will contact you and ask a fix for their device.
So, you'll read the specs, make an emulator, add the specific drawable/values and rerelease your app. Keep in mind that TABLETS will require special drawable/values folders.

Related

dimen.xml is not being executed after specifying it for other sizes

In my project I just had a dimens.xml file and after completing my project I decided to create dimens files for other screen sizes.
So I began with creating values-mdpi directory and placing a dimen.xml file in it. but now every dimen resource I define in values-mdpi dimens file applies to all screens!
In other word the general dimens file is being ignored.
But if I define value-xxxhdpi an place the resources for my device in there is works fine. But why?
If I don't define a dimens file for specific density it must take resources from general dimens.xml, must not?
Density-specific resources will match the lowest-matching type.
So, if you make a values-mdpi folder, any device that's MDPI or greater (most devices) will use any existing values in that folder, and fall back to the default otherwise.
Your values-xxxhdpi folder likely doesn't apply to your device because your device doesn't match the XXXHDPI spec. It might be XXHDPI or XHDPI.
You should switch your logic. Instead of creating special layouts for low-density devices, create special layouts for high density devices.
Please make a folder named as "values-nodpi" and put general in that it should work there ! this should help!
NOTE!:its better to do it this way though android does provide support
for highdpi folders and also automatically regress it out for lower
ones but it affects performance

what is dimens.xml(w820dp) in android studio.?

Upon creating the new project in android studio in the following path were created two xml files by default.
..res/values/dimens.xml
..res/values/dimens.xml(w820dp)
Am aware of the file dimens.xml where we can define the dimensions for the elements, but why the second file dimens.xml(w820dp) created by default.
Can someone please shed lights on why it is needed and when we can use it.
You can define several folders for each desired screensize in Andorid Studio. In your case, android studio defines values folders where you define dimensions (measurements) for scaling tablets or phones.
E.g. it defines two values folders like:
values-large
values-xlarge
or in your case
values-w820dp
Now inside this folders, a dimen.xml file is defined, where you can put your measurements in (unit dp) for the corresponding screensize.
Define a measurement like this:
<dimen name="value1">17dp</dimen>
Then embedd this sizes in your layout xml, like:
android:layout_height="#dimen/value1"
Depending on the screensize, the system will load the correct measurements from this folders, e.g. if you have a screen size large only the defined values in folder values-large will be loaded.
For more information, also have a look at https://developer.android.com/distribute/essentials/quality/tablets.html
EDIT1: So basically if you see the file dimen.xml(w820dp), the values in there are only used, if the app is executed on displays with 820dp and above, in this case, it means tablets.
In fact you have not file named ..res/values/dimens.xml(w820dp) at all in your project structure (check yourself). What you have instead is ..res/values-w820dp/dimens.xml file but to make editing right file easier, Android studio will (in Android view) show it as such. This file will be used by the framework on devices with display width 820dp or higher:
Specifies a minimum available screen width, in dp units at which the
resource should be used—defined by the value. This configuration
value will change when the orientation changes between landscape and
portrait to match the current actual width.
On other, "plain" dimens.xml will be loaded. See Providing Alternative Resources documentation on resource qualifiers to find out more about possible options and benefits.
Main use case of these files is to Support variety screen resolutions for user interface
dimens.xml
Android automatically take values from this file for device with dpi less than 820dp
dimens.xml(w820dp)
Android automatically take values from this file for device with dpi greater than 820dp
How to use
Declare dimension in these files with same name(dimens.xml)
<dimen name="button_width">200dp</dimen>
<dimen name="button_height">40dp</dimen>
Change only in their values (dimens.xml(w820dp))
<dimen name="button_width">400dp</dimen>
<dimen name="button_height">80dp</dimen>
w820dp
for screens with more than 820dp of available width. This would include 7"
and 10" devices in landscape (~960dp and ~1280dp respectively)
w820dp is a qualifier value to provide alternate values for different dimensions. This is used to localize to device types properly.

In Android, why do you need the resource qualifier -mdpi if it is already the default?

Say you have a dimens.xml file that goes in the res/values folder of your project. Then, you want to have some different dimensions based on screen density. So, for example, you can create res/values-hdpi/dimens.xml and res/values-xxhdpi/dimens.xml and have specific data for those configurations.
My question is, is there ever a reason to use res/values-mdpi? As I understand it, mdpi is the default density. Any values in the default values folder is considered mdpi so why is there ever a need for having values-mdpi?
Although I can think of a reason why you might have extra qualifiers such as values-en-mdpi, I don't see how values-mdpi is ever necessary when you can just put them in values.

Where is the documentation on resource alias syntax and limitations?

I have an app that uses the same layout for all screen sizes (portrait only). The only difference is a handful of dimensions -- text sizes, margins, padding, etc. -- that vary slightly from one screen size bucket to another. Accordingly, I use a single layout xml file stored in the default layout folder, and have separate dimen.xml files stored in size-specific folders named with the minimum-size qualifiers (values-sw600dp, etc.)
But the Android documentation on Supporting Multiple Screens says that the minimum screen-width qualifiers don't work below Android 3.2. So I need to put dimen.xml files in values-small, values-large, values-xlarge, etc.
In order to avoid having to change the dimen.xml file for the same size (e.g., large and sw600dp), in two places, I'd like to alias one of the references. The very limited documentation I've found on using resource aliases doesn't make it clear whether I can alias a dimen.xml file, or what the correct syntax would be.
My preference is to store the actual dimen.xml file for a given size in the minimum-width folder (e.g., in values-sw600dp) and put an alias in the dimen.xml file in old-style size folder (e.g., in values-large). I think this would be the syntax:
<resources>
<item type="values" name="dimen">#values-sw600dp/dimen</item>
</resources>
Or would it be this:
<resources>
<item type="values-sw600dp" name="dimen">#values/dimen</item>
</resources>
I can't find any documentation on the syntax for an alias like this, or how the type field is interpreted. Is there any specific documentation like that?
And, is it even possible to do what I want? Can I alias a dimen resource? Is there a limitation such that I would have to store the master dimen.xml file in the default values folder (for example, "dimen_large.xml") and use aliases to refer to it in dimen.xml files in each of the two size folders?

Different dimension for different language

I'm working on a project, where in the textview the dimension of text is mentioned in a file main_dimens.xml in values-1024x768 folder. But i need a different text size for specific language(say pt).
<dimen name="main_digitalclock_size">18dp</dimen>
I need 12dp for pt language. So i tried to add the above line of code in strings.xml of values-pt folder, also created a folder values-pt-1024x768 and putted in it. But still size is 18dp only.
Are you sure you named your resource folders correctly? I looked up the rules that the Android OS uses when determining which resources to use here:
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
It looks like the way to specify the usable screen size may be different from what you're using.

Categories

Resources