39

I'm trying to write some code that will reference a bool.xml file and will reference the current value inside the bool.

<bool name="enableQAurl">true</bool>

With this I want to be able to reference this in code, so that if it's set to True it does something and if false does something else. Just a simple If and else statement.

Any code references or feedback is greatly appreciated.

rmtheis
  • 5,992
  • 12
  • 61
  • 78
Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79
  • Related post - [Android: How do I get string from resources using its name?](https://stackoverflow.com/q/7493287/465053) – RBT Aug 14 '18 at 07:36

2 Answers2

81
 Resources res = getResources();
 boolean enableQAurl = res.getBoolean(R.bool.enableQAurl);

Source:
http://developer.android.com/guide/topics/resources/more-resources.html

kaderud
  • 5,457
  • 2
  • 36
  • 49
  • 1
    Interesting! On the contrary, to get a string from the resources is very straightforward - `getString(R.string.pref_show_bass_key)`. Initially, I thought something similar `getBool(R.bool.enableQAurl)`will work for boolean resources also but it didn't. This is a bit longer route to go through `Resources` class. – RBT Aug 14 '18 at 07:32
5

The above answer of kaderud's will work perfectly. If you are not in Activity, you have to use your context.

If you are in fragment or adapter then you have to follow below.

boolean enableQAurl = context.getResources().getBoolean(R.bool.enableQAurl);
Manaus
  • 407
  • 5
  • 9
Abish R
  • 1,537
  • 3
  • 18
  • 36