Fill data from arrays.xml to Spinner and ArrayAdapter : Spinner « UI « Android






Fill data from arrays.xml to Spinner and ArrayAdapter

  

package app.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class Test extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
          
        Spinner spinner = (Spinner)findViewById(R.id.spinner);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
                R.array.planets, android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);
    }
}
//main.xml

<?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"
    >
<Spinner android:id="@+id/spinner"  
    android:layout_width="wrap_content"  android:layout_height="wrap_content" />
</LinearLayout>


//arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="planets">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
    <item>Jupiter</item>
    <item>Saturn</item>
    <item>Uranus</item>
    <item>Neptune</item>
  </string-array>
</resources>

   
    
  








Related examples in the same category

1.Using Spinner
2.Spinner on selection listener
3.Disable and enable Spinner
4.Fill data to Spinner with ArrayAdapter
5.Fill data from arrays.xml to Spinner
6.Spinner Test
7.Spinner Item Selected Listener