I usually store dimensions in dimen.xml file. However, IntelliJ IDEA's intellisense places dimensions into file dimens.xml.
Now, what if I place dimensions into the file dimens.xml, how will Eclipse handle this?
I see that docs allow both files to be used. Which one should be used if the project will be used by both Eclipse and IDEA users?
It doesn't really matter what you name the file itself, however there are recommendations. From the docs (on res/values):
Because each resource is defined with its own XML element, you can
name the file whatever you want and place different resource types in
one file. However, for clarity, you might want to place unique
resource types in different files. For example, here are some filename
conventions for resources you can create in this directory:
arrays.xml for resource arrays (typed arrays).
colors.xml for color values
dimens.xml for dimension values.
strings.xml for string values.
styles.xml for styles.
Edit: For clarity, the important part is that you use the appropriate element (<dimen> in this case) inside a <resource> tag in your file. This is how android knows it's a dimension resource.
<resources>
<dimen name="my_dimen">10dip</dimen>
</resources>
Related
I have 4 different values folder in my res folder with different dimens.xml inside it. Each dimens.xml file have different values. The folders i got was the following:
values/dimens.xml
values-sw320dp/dimens.xml
values-sw480dp/dimens.xml
values-sw600dp/dimens.xml
The problem is when I run my app, it seems that the value used is the dimens.xml values stored in values-sw320dp/dimens.xml even when I run it in my Google Nexus 5 emulator.
Example usage is this:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Account "
android:textSize="#dimen/common_textSize_n" />
What am I doing wrong?
Or what is the proper way to use different dimens values for different screen sizes?
Any help would be greatly appreciated. Thank you very much!
I apologize that this question seems have multiple duplicates. I just didn't find the answer to the already asked questions.
You have to put different value in each folder.and dimens.xml will be the default one,if any of the dimension is missing it will take form there. and all other cases it will take the values according to the screen size. what you are doing is correct ,also you can add more dimens for mdpi,xhdpi,hdpi,xhdpi etc.
Rename the folder names to values-w max_size dp
E.g. :-
values-w320dp
values-w600dp
values-w820dp
Every folder will have one file namely dimens.xml.
Lets say an example if you have "feed_title_size" dimen, using in any layout file.
You want to set the textSize with this dimen but for different screen resolutions. then you need to create dimens.xml file in every resolution folder(e.g. values-w320dp)
and create a dimen attribute in it with "feed_title_size" name inside the resources tag.
So "feed_title_size" will present in multiple dimens.xml file and the appropriate value for it will be selected automatically on the basis of the screen resolution.
Here is the dimens.xml in values-w320dp folder looks like:
<resources>
<dimen name="feed_name_handle_width">100dp</dimen>
</resources>
Here is the dimens.xml in values-w640dp folder looks like:
<resources>
<dimen name="feed_name_handle_width">150dp</dimen>
</resources>
Here is the original dimens.xml file which contains in values folder. This file is default file.
<resources>
<dimen name="feed_text_width">50dp</dimen>
</resources>
Here is your layout file which contains your TextView
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/feed_text_width" />
I would like to load dimens.xml based upon the screen size, but use the same colors regardless of screen size.
I have the following directory structure
res
res/values
colors.xml
res/values-small
dimens.xml
res/values-normal
dimens.xml
res/values-large
dimens.xml
res/values-xlarge
dimens.xml
I am new to Android but it seems that the system picks only a single values directory at load time. What this means is that, although I get the dimens.xml from the values-qualifier directory as desired, I have no colors defined.
Is there some way to define certain kinds of values resources that are used for any device configuration, while defining other kinds of values resources on a per-configuration basis?
That directory structure should work as it is. From the documentation for Providing Resources
Whereas XML resource files in other res/ subdirectories define a
single resource based on the XML filename, files in the values/
directory describe multiple resources. For a file in this directory,
each child of the resources element defines a single resource. For
example, a element creates an R.string resource and a color
element creates an R.color resource.
That means that unless you have a particular color in (say) the values-large directory, the value from values will be used for it. The granularity is per-resource, and the names of the actual files are unimportant.
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 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?
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.