Friday, 2 January 2015

Android Load Your String Resources Programmatically



How do I Load Your String Resources Programmatically from a Class file and not an Activity:


this is in my Strings.xml



<!-- FactBook Resource -->
<string-array name="facts">
<item>facts 1</item>
<item>facts 2</item>
...
</string-array>


Here my Class File



public class FactBook {

public String[] mFacts;

mFacts = getResources().getStringArray(R.array.facts);

public String getFact() {

String fact = "";

// Randomly select a fact
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(mFacts.length);

fact = mFacts[randomNumber];

return fact;
}
}


I get a erro. - Invalid Method declaration, return type required.


No comments:

Post a Comment