Android: integer from xml resource
Yes it is possible, it would look like this:
Create an xml resources file in the folder
/res/values/
called integers.xml.You are free to give it any name as you want, but choose one that is obvious.
In that resources file, create your integer values.
Your file then looks something like that:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="maximum">100</integer>
...
</resources>Reference the integer value in the Java code like this:
It's a bit different from the
getString()
, you have to take a little detour.ProgressDialog progressBar = new ProgressDialog(getContext());
int max = getContext().getResources().getInteger(R.integer.maximum);
progressBar.setMax(max);
How to get Integer value from res/integers.xml?
You're looking at the android.R.integer
instead of your.namespace.R.integer
.
Eclipse probably imported the wrong one; it does that sometimes, it's rather annoying.
Go to your imports at the top of the file and remove:import android.R;
Then you should be able to use the quick-fix to add the correct import.
Change int from xml Resource
First thing's first Resources CAN NOT be Changed or Modified once an apk has been built. You can only add values to resources that need not to be modified.
So what to do? Preferences allow you to save and modify values at run time. This SO explains how to use Shared Preferences
in your app.
Have a look at this for easy to use Utility
for Shared Preferences
.
Accessing integer resources in xml
Finally I am able to access integer resource in java code and integer value as dimen in xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="input_field_padding">20dip</dimen>
<integer name="input_field_padding">20</integer>
</resources>
In xml file:-
<EditText
android:id="@+id/user_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="@dimen/input_field_padding" />
In java file:-
EditText mUsername = (EditText) this.findViewById(R.id.user_name);
//int padding = this.getResources().getInteger(R.integer.input_field_padding);
int padding = (int) this.getResources().getDimension(R.dimen.input_field_padding);
mUsername.setPadding(padding, padding, padding, padding);
how to get integer value from strings.xml in android
First, don't put them in strings.xml
. Put them in integers.xml
.
Second, you should use context.getResources().getInteger(R.integer.GUI_OK)
, where context
is an instance of a Context object, such as an Activity or Service.
R.integer.GUI_OK
is simply a resource value which Android uses to retrieve the actual value of that resource.
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"
How to use integer resources within string resources in android
short version is that you can't mix resources like this, but you can use in Java:
getResources.getString(R.string.some_string,
getResources.getInteger(R.integer.some_integer)
);
and then in your String XML
<string name="some_string">This is my string num %d in a row</string>
that way the %d
get replaced by the integer you pass in the getString
Getting different integer value from resource folder in Android
The device density is 165dpi
thats make it mdpi
device. The resolution is 320px x 480px
and because its mdpi
thats mean. The device is 320dp x 480dp
.
And from Google doc you can see this device is Normal
.
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
For me I would prefer to use sw-xxx
for example (you need to check the values):
res/values => 3
res/values-sw330 => 4
Related Topics
Alarmmanager Android Every Day
Interstitial Admob Ads: "Illegalstateexception: Only Fullscreen Activities Can Request Orientation"
How to Tell If a Closed Path Contains a Given Point
Android Spinner with Different Layouts for "Drop Down State" and "Closed State"
Android Google+ Integration - Repeated Userrecoverableauthexception
Android Upload Video to Remote Server Using Http Multipart Form Data
Firebaseinitprovider: Firebaseapp Initialization Unsuccessful
How to Combine Multiple Images into a Single Image in Android
How to Create Converter for My Class in Android Retrofit Library
Android and Setting Alpha for (Image) View Alpha
How to Populate Android Room Database Table on First Run
Pattern "One Activity, Multiple Views": Advantages and Disadvantages
Why Emulator Is Very Slow in Android Studio
How to Set the Output Image Use Com.Android.Camera.Action.Crop