Bencri
Bencri

Reputation: 1283

How can I share a int value globally (xml and code)?

In my Android app, I use LinearLayout's weight at multiple point in my app. Those weight are both used in xml and in code as I change them dynamically.

To reuse the same values everywhere, I thought I could declare them in my dimens.xml file like this:

<resources>
    <dimen name="total_weight">5</dimen>
    <dimen name="expanded_weight">3</dimen>
    <dimen name="collapsed_weight">2</dimen>
</resources>

But it doesn't work as dimen only accept dp, px or sp units.

What workaround could I use?

Upvotes: 0

Views: 37

Answers (1)

Siwakorn Petchuchuay
Siwakorn Petchuchuay

Reputation: 176

you can use

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

access by

resources.getInteger(R.integer.integer_name)

you can take a look at https://developer.android.com/guide/topics/resources/more-resources

Upvotes: 1

Related Questions