Friday, March 23, 2012

How can we retrive string array from xml in android

How can we retrieve string array from xml  in android

1,Define the following to the strings.xml:

   <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="hello">Hello World, StraryActivity!</string>
    <string name="app_name">Strary</string>
    <string-array name="ary"><item >jisho</item>
    <item >jino</item>
    <item>aju</item>
    </string-array>
    </resources>

2, Access the resource in activity using the following code:

   
    Resources res = getResources();
    String[] planets = res.getStringArray(R.array.planets_array);  
 
 
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.Toast;

public class StraryActivity extends Activity 
{
    /** Called when the activity is first created. */
 int i;
 String n;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources res = getResources();
        String[] ary = res.getStringArray(R.array.ary);
        
        for(i=0;i<=ary.length-1;i++)
        {
        Toast.makeText(this,ary[i],Toast.LENGTH_LONG).show();
        }
        
    }
} 


No comments :

Post a Comment