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
Detect When Recyclerview Reaches the Bottom Most Position While Scrolling
Compiling the Latest Openssl for Android
How to Display Input Errors in Popup
What Is the Use of the Res/Values/Public.Xml File on Android
Navigation Drawer Semi-Transparent Over Status Bar Not Working
Fragments Onresume from Back Stack
Jni and Gradle in Android Studio
Android - Custom UI with Custom Attributes
How to Add a Fast-Scroller to the Recyclerview
How Is Staticlayout Used in Android
How to Change Background Color in Android App
Version Conflict Updating to 8.4.0
Disconnect a Bluetooth Socket in Android
Toolbar Overlapping Below Status Bar
Difference Between APK (.Apk) and App Bundle (.Aab)
Why Do Most Fields (Class Members) in Android Tutorial Start with 'M'
Error: No Toolchains Found in the Ndk Toolchains Folder for Abi with Prefix: Llvm