How can I set margins(Left,Top,Right,Bottom) to these ImageView so that it will display correctly on multiple screen sizes in Android ? Also I need to handle click events on these Images.1
Well the whole point of using DP is so that you don't have to worry about this. Margins will be roughly the same across devices, but if you're relying on lining things up on one particular device resolution/density combination, you'll definitely be in for a surprise when you test on other devices.
That said, if you do need to specify different margins for different screen sizes, simply add an XML file in res/values -- something like dimens.xml:
<resources
xmlns:android="http://schemas.android.com/apk/res/android"
>
<dimen name="my_view_margin">10dip</dimen>
</resources>
Then add one of these XMLs for every specific device qualifier that you need to (e.g. values-large, values-sw600dp, values-xlarge, etc.) and modify the value as you see fit. When you want to use these dimensions in a layout, just use:
android:layout_margin="#dimen/my_view_margin"
And Android will pick the correct value for whatever device it happens to be running on.
provide different values folders and set values.
For Ex.
values,
values-sw350dp,
values-sw480dp,
values-sw600dp,
values-sw720dp etc.
Related
Hello I am using different textsizes depending on different screen sizes.
Like:
values-small: dimens.xml -> ?
values-normal: dimens.xml -> 10sp
values-large: dimens.xml -> ?
values-xlarge: dimens.xml -> ?
I want to know if there is a formula to set the right sizes depending on small, large, xlarge instead of setting sp size by testing it on different screens^^?
second question: is values-normal neccessary, isnt "values" folder == values-normal?
third question: what happens when I declare dimens.xml for small, normal, and large and the device is a XLARGE device will it use dimens.xml from values? or valuse-normal? or the next smaller one values-large?
thank you
I want to know if there is a formula to set the right sizes depending on small, large, xlarge instead of setting sp size by testing it on different screens
Not really. Many apps do not change their font size at all based on screen size, just as most Web apps do not change their font size based on browser window size. Of course, you are certainly welcome to specify different <dimen> resources for different screen sizes, including dimensions for use with text. And there's nothing stopping you from using an algorithm like the others suggest; just understand that there's no real reason to use that algorithm. To put it another way, a graphic designer should be telling you how big to make the text, not a calculator.
Moreover, the -small, -normal, etc. buckets are not used as much anymore, in favor of the more flexible -wNNNdp, -hNNNdp, and -swNNNdp buckets.
isnt "values" folder == values-normal?
No.
Suppose you have res/values-small/, res/values/, res/values-large/, and res/values-xlarge/. Further suppose that each resource set defines a text_size dimension resource, and you use that in in layouts (e.g., #dimen/text_size).
A -normal device will then pull from res/values-small/, as -small is for small screens or larger. Your res/values/ version of the resource will never be used. Hence, the typical pattern would be not have res/values-small/, putting your -small resources in res/values/, and overriding that default value in res/values-normal/, res/values-large/, and res/values-xlarge/.
what happens when I declare dimens.xml for small, normal, and large and the device is a XLARGE device will it use dimens.xml from values? or valuse-normal? or the next smaller one values-large?
It should pull from res/values-large/, as that is the closest match among the qualifying resource sets.
This is the common problem in majority of Android projects to support varied screen sizes.
In short, you can make use of scalable DP and scalable Font approach.
Android project structure inherently allows specifying different font size (SP) and margin values (DP) by defining them in dimens.xml files for supported screen sizes. But the problem that still remain is - what values to be used ? UX designers normally provide style guides for only one set (e.g. 360x640 DP screen size) and don't provide for all other supported screen sizes. So the problem to use right vlaues for other supported screen sizes still remain unanswered.
One good solution (per my understading) is to use readymade scalable dictionary of dimens.xml files, something similar to the dictionary of files provided by this library. After adding these xml files to project, you just need to use the keys in your layouts, then Android automatically uses font/margin value from respective directory. Please refer here for visual difference - with and without this approach.
I use the ratio 0.75 : 1 : 1.5 : 2 same as for DPIs. It works fine.
I want to use different font size for different screen sizes.
I read about this many articles, but I'm not sure about usage. Is correct to use different dimens resource file for different screen dimensions like code below:
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">24sp</dimen>
</resources>
...
I also read that correct way is to use sp for font size, but this doesn't fit font in different screen dimensions as I expect.
If there, what are the disadvantages of using different dimens for every size?
Thanks
The best way is to create different layout resources for each of the screens you wish to support. Place each of the layouts in a separate folder that designates the width of the screen. For example, normal sized layouts go in your res/layout folder, and a layout resource for a 7 inch tablet (600 pixel width) would go in the res/layout-sw600dp folder. Make the resource names identical, but adjust your font sizes accordingly.
#up Not, it isn't good way.
#topic You can gets width & height of screen (and w&h of View). Next, you can set font, for example 2% of width screen. If you have content 1260x720, 0.02*1260=24,6 px (you can use also (int)24.6 to convert double to int)
I do something very similar and it's worked fine for me. Some people put the values in the various layouts such as Fietser suggested, but if all of your layouts end up being the exact same except for the font size, your approach is better. That way you can have a single layout and only modify the font sizes. But sometimes you might have changes in the actual layout xml, so then it's probably a wash between the two approaches.
I have a normal-hdpi-480x800 layout, which includes buttons and similar graphics. I mostly use RelativeLayout, since it's recommended for better performance, and position my elements from the upper left bound relatively to each other. According to screens_support.html this group might also include 600x1024 resolution devices. So, when I test the application on LG L9 (540x960), which also falls into normal-xhdpi, it looks horrible - it seems there's no difference between using pixels and dp. Here's an example of code:
<Button
android:id="#+id/fb_post_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/share_fb_m_l"
android:layout_marginTop="#dimen/share_fb_m_t"
android:background="#drawable/fb_btn" />
Any help ? What I might be doing wrong ?
Thanks.
Just specify different dimensions in folders values-ldpdi, values-mdpi, values-hdpi, values-xhdpi if you need more control over button sizes according to screen density. Like this:
<resources>
<dimen name="share_fb_m_l">47dp</dimen>
<dimen name="share_fb_m_t">57dp</dimen>
</resources>
Update: You can get more control over this:
Specify multiple different images for different screen resolutions and densities. To do so read supporting multiple screens. If you need even more precision in image scaling and quality you can use the fundamental size of screen attribute which is swdp - you can specify the smallest width of the screen where your image should be used. This is a qualifier name for a resource folder.
Place your dimensions in these folders values-sw540dp, values-sw540dp, values-sw600dp - these should serve as subfolders for more control.
In my layout XML files, I reference a lot of parameters through a separate files called dimens.xml.
For example, dimens.xml contains parameters like these:
<dimen name="textSize_normal">20dp</dimen>
<dimen name="buttonTextSize_normal">15dp</dimen>
<dimen name="editTextSize_normal">17dp</dimen>
<dimen name="buttonHeight_normal">37dp</dimen>
<dimen name="margin_normal">5dp</dimen>
And in my main.xml, for example, I would set the text size for a TextView by doing something like this:
android:textSize="#dimen/editTextSize_normal"
It works great.
Now, my questions is, is it possible to set the values for the dimen variables in my dimen.xml file programmatically from my main activity? What I am trying to do is fetch the screen size, and set, for example, the textSize based on a fraction of the height of the screen so that it is easily adaptable to any screen size. I have all that figured out, I just need your help to figure out how to set the dimen variables in my code.
Now, my questions is, is it possible to set the values for the dimen variables in my dimen.xml file programmatically from my main activity?
No.
What I am trying to do is fetch the screen size, and set, for example, the textSize based on a fraction of the height of the screen so that it is easily adaptable to any screen size.
Ignoring that this is an odd UI approach, the way to do that is to delete your android:textSize attributes and to change the text size at runtime using setTextSize() in Java.
I have all that figured out, I just need your help to figure out how to set the dimen variables in my code.
You don't "set the dimen variables". You apply your calculations to the widgets, via setters like setTextSize().
No, you can't edit the Resources files on runtime, they are already compiled and generated.
You should use density independent pixels in all your resources, so that dimensions can adapt to screen size. You don't need to calculate that values at runtime. If you want to have a different layout for different screen sizes, then consider using multiple resource files.
Read this guide.
i guess what you mean is to change the size depending on the device's screen size..
which can be made by creating multiple dimens.xml files in folders like values-hdpi, values-xhdpi, values-xxhdpi, values-mdpi,.. and then on runtime the compiler will choose the dimen depending on the dpi
i actually didn't try this before but i read it
here.. take a look at this:
http://developer.android.com/guide/practices/screens_support.html#qualifiers
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