Randomly select a string from strings.xml in Android

android

Solution

Create an array contains all of your resource names you want to select:

`String[] strs = new String[] {"str1", "str2", "str3"};`

Get a random index:

`int randomIndex = new Random().nextInt(3);`

Get your random string from resource:

`int resId = getResources().getIdentifier(strs[randomIndex ], "string", your_package_name);`

`String randomString = getString(resId);`

Problem

I need to randomly select a string defined within strings.xml file in android. For example my strings.xml is : ``` <resources> <string name="str1">Content comes here1</string> <string name="str2">Content comes here2</string> <string name="str3">Content comes here3</string> </resources> ``` Can I randomly select one of these strings in my Activity?

Original source