Load Dimension Value from Res/Values/Dimension.Xml from Source Code

Load dimension value from res/values/dimension.xml from source code

In my dimens.xml I have

<dimen name="test">48dp</dimen>

In code If I do

int valueInPixels = (int) getResources().getDimension(R.dimen.test)

this will return 72 which as docs state is multiplied by density of current phone (48dp x 1.5 in my case)

exactly as docs state :

Retrieve a dimensional for a particular resource ID. Unit conversions
are based on the current DisplayMetrics associated with the resources.

so if you want exact dp value just as in xml just divide it with DisplayMetrics density

int dp = (int) (getResources().getDimension(R.dimen.test) / getResources().getDisplayMetrics().density);

dp will be 48 now

Load value from dimens.xml and make it negative

That is not possible in standard layout XML. You could retrieve the dimension value in Java, multiply it by -1, and apply it in Java.

You could even do that in the form of a custom attribute (e.g., yourapp:layout_negativeMarginRight) in your own custom subclass of ViewGroup, if you really wanted. This would seem to be overkill.


UPDATE: This is now somewhat possible via data binding expressions:

android:padding="@{0.75f * @dimen/icon}"

It does not work for margins or any other layout attributes due to the lack of binding adapters which you can implement. See this bug.

Get integer value from dimens.xml resource file in Android

Why not store the integer in res/integers.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="quantity_length">12</integer>
</resources>

And to access the values in code

int myInteger = getResources().getInteger(R.integer.quantity_length);

Or in XML

android:maxLength="@integer/quantity_length"

Is it possible to set dimension value resources as a calculation of other dimension values in android XML?

programtically you can do it. In xml layout you cant ...so why not just get the dimension programatically and do your calculation ?

if you really need something like that an ant script might help you but thats really complicated for such a little change.

How to get a dimension value that is a reference to an attribute?

This is what I implemented but it feels cumbersome and hacky:

private int getDimension(@DimenRes int resId) {
final TypedValue value = new TypedValue();
getResources().getValue(resId, value, true);

if (value.type == TypedValue.TYPE_ATTRIBUTE) {
final TypedArray attributes = getTheme().obtainStyledAttributes(new int[]{value.data});
int dimension = attributes.getDimensionPixelOffset(0, 0);
attributes.recycle();
return dimension;
} else {
return getResources().getDimensionPixelOffset(resId);
}
}

I was hoping that the framework would dereference my ?attr/ dimension directly.

How change dimension programmatically?

It is impossible to set the values for the dimen variables in your dimen.xml file programmatically from the activity.

The way to do what you want is to delete your android:textSize attributes and to change the text size at runtime using setTextSize() in Java.



Related Topics



Leave a reply



Submit