Android: integer from xml resource

How do I change my XML resources, or which XML file should I create to access integer values ​​the same way you access string values ​​using R.string.some_string_resource ?

For example, in the code I want to say:

 ProgressDialog progressBar = new ProgressDialog(getContext()); progressBar.setMax(getInteger(R.integer.maximum)); 

Is it possible?

+62
android android-resources
Oct 10 '13 at 13:45
source share
1 answer

Yes, perhaps it will look like this:

  • Create an xml resource file in the /res/values/ folder called integers.xml.

    You can give it any name you want, but choose what is obvious.

  • Create integer values ​​in this resource file.

    Then your file looks something like this:

     <?xml version="1.0" encoding="utf-8"?> <resources> <integer name="maximum">100</integer> ... </resources> 
  • A reference to an integer value in Java code as follows:

    This is slightly different from getString() , you need to get around a bit.

     ProgressDialog progressBar = new ProgressDialog(getContext()); int max = getContext().getResources().getInteger(R.integer.maximum); progressBar.setMax(max); 

Hope this helps anyone who has the same question.

+156
Oct 10 '13 at 13:45
source share



All Articles