Populate Second Spinner Based on Selection of First Spinner

This example shows how to populate second spinner based on selection of first spinner.

XML: Layout File Entry
res/layout/activity_spinner_ex4.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="8dip"
        android:text="@string/lblAcc" />
     
    <!-- Spinner Dropdown -->

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
        android:layout_marginTop="10dip"
        android:entries="@array/acc_type" />
     
    <!-- Select Label -->

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="8dip"
        android:text="@string/lblSubAcc" />
     
    <!-- Spinner Dropdown -->
    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
    />
</LinearLayout>

XML: Strings File Entries
res/values/strings.xml

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

    <string name="app_name">Spinner Example</string>
    <string name="action_settings">Settings</string>
    <string name="lblAcc">Select Account Type</string>
    <string name="lblSubAcc">Select Account Head</string>

    <string-array name="acc_type">
        <item>Income</item>
        <item>Expense</item>
    </string-array>

</resources>

Main Activity
SpinnerEx4Activity.java

package com.bar.example.androidspinnerexample;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class SpinnerEx4Activity extends Activity implements
OnItemSelectedListener{
Spinner s1,s2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spinner_ex4);
		s1 = (Spinner)findViewById(R.id.spinner1);
		s2 = (Spinner)findViewById(R.id.spinner2);
		s1.setOnItemSelectedListener(this);
	}
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		String sp1= String.valueOf(s1.getSelectedItem());
		Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
		if(sp1.contentEquals("Income")) {
			List<String> list = new ArrayList<String>();
			list.add("Salary");
			list.add("Sales");
			list.add("Others");
			ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
			dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			dataAdapter.notifyDataSetChanged();
			s2.setAdapter(dataAdapter);
		}
		if(sp1.contentEquals("Expense")) {
			List<String> list = new ArrayList<String>();
			list.add("Conveyance");
			list.add("Breakfast");
			list.add("Purchase");
			ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
			dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			dataAdapter2.notifyDataSetChanged();
			s2.setAdapter(dataAdapter2);
		}
		
	}
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
}

Emulator Screenshot:
android_spinner_ex4

You can download all android example from here.

 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

39,383` Total Views

One comment

  • what is you have 3 spinner ?

    for exemple :

    first -> 3 default values
    second -> 9 default values, 3 for each item from the first spinner
    third -> 18 values, 2 for each item from the second spinner

Leave a Reply

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


five × = 10