Kalina
Kalina

Reputation: 5594

How to store ints for different screen sizes in a .xml?

Similar to how you can have two folders res/values and res/values-xlarge, and in each you can have a dimens.xml file with different dimensions...

I want to be able to store some integers (not sizes in dp or anything like that, just plain integers) like that. Is this possible to do in .xml? Or if not, how could I do that programmatically?

If you must know why: I am displaying a list where the user can choose to show the number of results at a time. For smaller screens, I want the default list length to be smaller than the default list length for larger screens.

Upvotes: 0

Views: 1072

Answers (3)

kabuko
kabuko

Reputation: 36302

Yes, there's a mechanism exactly like that. Just create an XML file in res/values and related folders and instead of something like:

<resources>
    <dimen ...>
</resource>

You can provide an integer resource:

<resources>
    <integer name="i">234</integer>
</resource>

However, in your specific scenario I'd also consider if simply changing the related layout file makes more sense.

Upvotes: 5

Christian Orth
Christian Orth

Reputation: 4653

Sure, you can do that. Just like storing a string Ressource you can store Integers.

Taken from the documentation:

XML file saved at res/values/integers.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="big_screen_width">12000</integer>
    <integer name="small_screen_width">320</integer>
</resources>

And the code would look like that

Resources res = getResources();
int bigScreen = res.getInteger(R.integer.big_screen_width);

Upvotes: 4

Heskja
Heskja

Reputation: 785

You can store things programmatically by using SharedPreferences.

Upvotes: 1

Related Questions