Get string array from strings.xml in android

Android string xml array to listview arrayadapter example.

On my this tutorial you are creating a string array in strings.xml file and call that string array in MainActivity.java programming file through getResources() function. This function helps us to import strings file array into string variable directly into activity. With the help of this tutorial developer can easily define array into strings.xml file and set string.xml array into listview. So here is the complete step by step tutorial for Get string array from strings.xml in android .

android-project-download-code-button

Get string array from strings.xml in android .

Note : Before start this project please create a string array on strings.xml file so read my post.
Code for MainActivity.java file.

 package com.android_examples.com.listviewsetarray;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

 ListView listView;
 String[] listValue;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 listView = (ListView)findViewById(R.id.listView1);
 
 // Function for assign string.xml file array to loacal array.
 listValue = getResources().getStringArray(R.array.mca_class);
 
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, listValue);
 
 listView.setAdapter(adapter); 
 
 }

 
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.listviewsetarray.MainActivity" >

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" >
 </ListView>

</RelativeLayout>

Code for Strings.xml file.

<?xml version="1.0" encoding="utf-8"?>
<resources>

 <string name="app_name">ListViewSetArray</string>
 <string name="hello_world">Hello world!</string>
 <string name="action_settings">Settings</string>

 
 <string-array name="mca_class">
 <item>I Sem</item>
 <item>II Sem</item>
 <item>III Sem</item>
 <item>IV Sem</item>
 <item>V Sem</item>
 <item>VI Sem</item>
 </string-array>
 
</resources>

Screenshot:

Get string array from strings.xml in android

Click Here To Download Get string array from strings.xml in android project.

Leave a Reply

Your email address will not be published. Required fields are marked *