Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to load string array value from string.xml in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 356
    Comment on it

    Using below code I have created a load string array value from string.xml. String is a simple resource using the value provided in the name attribute. So we can combine string resources with other simple resourecs in the xml file. Below code will clearly describe you how to load string arrayvalue from string.xml.

    Step(1)-Create a class-

    public class Test extends ListActivity {
      String[] presidents;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView lstView = getListView();
        // lstView.setChoiceMode(0); 
        // lstView.setChoiceMode(1); 
        lstView.setChoiceMode(2); 
        lstView.setTextFilterEnabled(true);
    
        presidents = getResources().getStringArray(R.array.presidents_array);
    
        setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_checked, presidents));
      }
    
      public void onListItemClick(ListView parent, View v, int position, long id) {
        parent.setItemChecked(position, parent.isItemChecked(position));
    
        Toast.makeText(this, "You have selected " + presidents[position],
            Toast.LENGTH_SHORT).show();
      }
    };
    

    Step(2)-main.xml layout-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
    </LinearLayout>
    

    Step(3)-string.xml-

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, MainActivity!</string>
        <string name="app_name">BasicViews5</string>
        <string-array name="presidents_array">
            <item>Evon/item>
            <item>HCL</item>
            <item>Wipro</item>
            <item>ORACLE</item>
            <item>Tcs</item>
            <item>Big Data</item>
            <item>Microsoft</item>
    
        </string-array>
    </resources>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: