Android Array in XML

Create an array in strings.xml file. Explain how will you access this array in Java code?

First create an array in App → res → values → strings.xml file.
<resources>
<string name = "app_name">AndGravity</string>
<string-array name = "WebsiteName">
<item>CareerRide</item>
<item>ABC</item>
<item>PQR</item>
<item>XYZ</item>
<item>Android</item>
</string-array>
</resources>

Following is the content of the modified MainActivity.java file
You can write the given below code in onCreate() or button click event.
Resources res = getResources();
String[ ] name = res.getStringArray(R.array.WebsiteName); //Name of the array
for(int i = 0; i<name.length; i++)
{
     str+=name[i]+" , ";
}
Toast toast = Toast.makeText(this,str,Toast.LENGTH_LONG);
toast.show();

Resource class is used to access the application's resources. By calling getResource(), you can initialize resource class. It is an abstract class. You can then access the array resource that is declared in stings.xml file by using getStringArray() method.