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.
Related
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
Currently, I'm developing Android app (phone only) and using only one size for different screen sizes, ie:
dimens.xml:
<dimen name="button_size">48dp</dimen>
<dimen name="text_size">16sp</dimen>
so in different screen sizes, we have only one size for components. And we go to this result: in small device, a textview can contain 10 chars but in larger device, a textview can contain 20 chars
And some developers use a gradle script to generate multiple dimens files in different folders based on the main dimens file like this:
values-sw320dp
dimens.xml:
<dimen name="button_size">48dp</dimen>
<dimen name="text_size">16sp</dimen>
values-sw480dp
dimens.xml:
<dimen name="button_size">52dp</dimen>
<dimen name="text_size">20sp</dimen>
...
so the system will use the dimens based on device size. And we go to this result: in small device and larger device, a textview can contain the same char, ie: 12 chars.
My question is: which one is better for UI, UX? (using Google Material Design)
You can use this library to support multiple screen dimen here
You can try this below, this will set automatically based on device.
?android:attr/textAppearanceMedium - For Medium font
?android:attr/textAppearanceSmall - For Small font
?android:attr/textAppearanceLarge - For Large font
Please check Material guidelines, To ensure usability for people with disabilities, give buttons a height of 36dp and give touchable targets a minimum height of 48dp.
Best practice is to use different dimens file for different devices. This will help you application view to be same across devices. If you keep same dimens for different devices then layout problem can also come. In some devices your layout will look perfectly fine but in another it will look very bad.
Android developer site also recommend to use different layout for supporting different devices.
I am using multiple values folders like values-hdpi ,values-xhdpi , values-xxhdpi folders to support multiple screen sizes. Lets say I have a dimension named "listitemheight" in all that values folders with different density pixel values.My question is, when I select between Nexus S(hdpi) and Nexus 5 (xxhdpi) Design rendering of Android Studio IDE does not pick specific dimension for the screen size .Is there any way to do that correctly ?
In values-hdpi
<dimen name="listitemheight">30dp</dimen>
In values-xxhdpi
<dimen name="listitemheight">70dp</dimen>
EDIT : It always chooses hdpi folder
Try using folders with names like "values-w600dp" . I had the same problem with you and this was the solution for me.
Starting with API Level 13 (Android 3.2), the screen sizes are deprecated in favor of using the swdp qualifier. This new qualifier declares the amount of space a given layout needs. It is strongly recommended that applications that are meant to run on Android 3.2 or higher should be using these newer qualifiers.
For example, if a layout required a minimum 700dp of screen width, the alternate layout would go in a folder layout-sw700dp
Source : http://developer.xamarin.com/guides/android/application_fundamentals/resources_in_android/part_4_-_creating_resources_for_varying_screens/
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.
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.