Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Booleans and Integers from strings.xml

Tags:

java

android

I've only ever had to work with Strings within the strings.xml file before, but now I have a case where I need to work with Boolean and Integer items.

<item type="integer" name="usenetPort">563</item>

<item type="bool" name="usenetUseSSL">true</item>

I usually use getResources().getString(R.strings.my_string) to retrieve a value, but that doesn't work with Boolean and Integers. I tried getInteger() and getBoolean() but Eclipse says that's wrong.

How do I get the value from Booleans and Integers?

like image 733
dotty Avatar asked Jul 26 '11 11:07

dotty


People also ask

How do you use Booleans with strings?

To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

How do you get string resources?

android:text="@string/hello" /> String string = getString (R. string. hello); You can use either getString(int) or getText(int) to retrieve a string.

How do you compare a boolean to a string?

Using the Strict Equality Operator (===) In this method, we will use the strict equality operator to compare strings to Boolean. The strict equality always returns false when we compare string and boolean values as it also checks for the data type of both operands.

How do you give a boolean value in properties file?

When the Properties of your file are loaded you can use the Boolean -Class to get the Properties: Boolean. getBoolean("your. property");


1 Answers

It works for me, first I openned Strings.xml and added this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <item type="integer" name="mynumber">4</item>
</resources>

Then in the code I get the value by doing this:

int number= getResources().getInteger(R.integer.mynumber);

You have to use R.integer.* and not R.string.*

like image 197
Daniel Novak Avatar answered Oct 31 '22 19:10

Daniel Novak