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" />
Related
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 want to develop an app for smartphones and tablets. Now my problem is that my layout isn't scalled in the right dimensens. On a 10' tablet it looks like this:
But this here are screenshoots from a 7' tablet and from a smartphone:
I would like to set a min width and height for the ImageView and scale it up and down depending on the screen size so in each layout the ListView is visible. Which unit do I have to use for the ImageView width and height? I tried with dp, in and some more but that does not help...
Use
<ImageView
android:layout_width="#dimen/imageViewWidth"
android:layout_height="#dimen/imageVewHeight"
android:scaleType="fitXY"
/>
scaleType="fitXY" will scale your image and fit with ImageView width and height. This will solve your problem.
Edit
In your values directory, open strings.xml file, then add following lines
<dimen name="imageViewWidth">30dp</dimen>
<dimen name="imageViewHeight">30dp</dimen>
Now, open values-sw600dp directory (of not, create one) and open strings.xml file (if not, copy from other values directory).
<dimen name="imageViewWidth">100dp</dimen>
<dimen name="imageViewHeight">100dp</dimen>
Now, open values-sw720dp directory (of not, create one) and open strings.xml file (if not, copy from other values directory).
<dimen name="imageViewWidth">120dp</dimen>
<dimen name="imageViewHeight">120dp</dimen>
This will solve your problem of different sizes of ImageView for different layouts.
Still can't understand, read my answer at
Android Universal App Approach
Layouts on different models of mobiles
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>
For instance, in a specific layout I have the following XML:
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
android:columnWidth="48dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="spacingWidth" />
This grid view is specific to this layout and I don't think I'll be using any other grid views with similar properties. That to say that the dimension values in the code are specific to that grid view.
Should I still move them to a dimens.xml file or it's fine to just leave them like that? If so, should I place values in the dimens.xml file only when that value is used across multiple layouts?
I drop dimension values into a dimens.xml resource typically for three reasons:
Reuse: I need multiple widgets or layouts to use the same value and I only want to change it once when updating or tweaking across the application.
Density Difference: If I need the dimension to be slightly smaller or larger from ldpi -> hdpi or small -> large.
Reading in from code: When I'm instantiating a view in the code and want to apply some static dimensions, putting them in dimens.xml as dp (or dip) allowing me to get a scaled value in Java code with Resources.getDimensionPixelSize().
Supplemental answer
#Devunwired lists 3 reasons to use dimens.xml. Here are the details of how to do that.
1. Reuse
If you set some dp or sp value in dimens.xml once like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_padding">16dp</dimen>
<dimen name="large_text_size">30sp</dimen>
</resources>
you can reuse it throughout your app in multiple locations.
<TextView
android:padding="#dimen/textview_padding"
android:textSize="#dimen/large_text_size"
... />
<TextView
android:padding="#dimen/textview_padding"
android:textSize="#dimen/large_text_size"
... />
Then when you need to make a change, you only need to do it in one place.
Notes
This is basically the same effect as using a style or theme.
Be careful not to give two different views the same dimen value if they really shouldn't be. If you need to make changes to one set of views but not another, then you will have to go back to each one individually, which defeats the purpose.
2. Size Difference
#Devunwired called this Density difference, but if you are using dp (density independent pixels), this already takes care are the density difference problem for all but the most minor cases. So in my opinion, screen size is a more important factor for using dimens.xml.
An 8dp padding might look great on a phone, but when the app is run on a tablet, it looks too narrow. You can solve this problem by making two (or more) different versions of dimens.xml.
Right click your res folder and choose New > Value resource file. Then write in dimens and choose Smallest Screen Width. Write in 600 for the width (7” tablet). (There are other ways of choosing the sizes. See the documentation and this answer for more.)
This will make another values folder that will be used for devices whose smallest screen width is 600dp. In the Android view the two dimens.xml files look like this.
Now you can modify them independently.
values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_default_padding">16dp</dimen>
</resources>
values-sw600dp/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_default_padding">64dp</dimen>
</resources>
When using your dimen you only have to set it with the name you used in both dimens.xml files.
<LinearLayout
...
android:padding="#dimen/my_default_padding">
</LinearLayout>
The system will automatically choose the right value for you depending on the device the user is using.
3. Reading in from code
Sometimes it is a pain scaling programmatically between px and dp (see this answer for how).
If you have a fixed dp value already defined in dimens.xml like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_dp_value">16dp</dimen>
</resources>
Then you can easily get it with
int sizeInPixels = getResources().getDimensionPixelSize(R.dimen.my_dp_value);
and it will already be converted to pixels for whatever density device the user has.
The dimens.xml file is used to keep all the hard-coded pixel values in one place.
Now, although you may not repeatedly use these values right now, it's still a good idea to to place them in dimens.xml for future reference. Besides, following a standard Android programming paradigm helps other developers to understand your code faster. This is much like the strings.xml where we place Strings some of which end up being used only once! :)
I don’t know if it can help you but I wrote a little java programe that allows you to duplicate a dimension xml file with a new desired value so that you no longer have to do it by hand line by line.
https://github.com/Drex-xdev/Dimensions-Scalable-Android