9

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.

2
  • 3
    Text sizes are normally set in scaled pixels (sp), so they adjust based on the default font size of the device. That default font size is based off a manufacturer setting and (on ICS and higher) a user override. If you use sp, your text size should adapt based upon screen size and user preference automatically, without you having to do some sort of percentage-of-screen-size calculations. Commented Aug 18, 2012 at 14:57
  • @CommonsWare Ok, that's fine, but I want to apply this to other things. Like image sizes for example. Commented Aug 18, 2012 at 15:04

4 Answers 4

12

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().

Sign up to request clarification or add additional context in comments.

2 Comments

I understand it is an odd UI approach, but what else can I do? You see, my problem is that I have layouts created for small, normal. large, and xlarge screen sizes. The problem I am running into is that if I create an EditText field, for example, and set its height to 50dp in a the normal layout. It looks as it should on a 3.2" screen, but it looks too small relative to the screen size on a 4" screen (which also qualifies for the normal layout file). How do I solve this problem any other way then using percentages of the screen size? Thanks, I appreciate your answer very much.
@capcom: "I understand it is an odd UI approach, but what else can I do?" -- set the height of the EditText using the appropriate attributes to have it show a certain number of lines of text, in whatever text size you have set, rather than hard-coding a height value. And then you leave it alone, rather than worrying about the differences in appearance on a 3.2" to a 4.0" screen.
7

No, you can't edit the Resources files on runtime, they are already compiled and generated.

3 Comments

Are you sure? I am able to override other things in XML layout files. For example, I very easily alter the image size of ImageViews at run time, regardless of what I had them initially set to in my XML file.
@capcom: "Are you sure?" -- I am very sure that you cannot modify dimension resources at runtime, or any other type of resource. "I very easily alter the image size of ImageViews at run time, regardless of what I had them initially set to in my XML file" -- that is Java. Java != resources. Resources are read-only at runtime.
You cannot CHANGE resources, but you can set anything in Java code like width or height - therefore override them logically.
1

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.

2 Comments

I already have four layouts setup, but the thing is even within a specific layout there are different screen sizes. For example, in a normal size layout, a 3.2" screen and 4.3" screen qualify. The both have a different number of dps as well, so it makes my app not fill up the entire screen if I designed it for 3.2".
@capcom: Usually you handle this via layout rules (e.g., android:layout_weight of a LinearLayout) rather than hand-calculating pixel counts.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.