Dynamic Spinner

Description:
This example will show how to create spinner with list view at run time in android. In this example you will be able to assign the contents of a spinner to another spinner at run time on a button click.

Algorithm:

  1. Create a new project by File-> New -> Android Project name it DynamicSpinner.
  2. Write following into main.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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="DynamicSpinner" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/createspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Create Spinner Dynamically" />

</LinearLayout>

 

  1. Run for output.

Steps:

  1. Create a project named DynamicSpinner and set the information as stated in the image.

Build Target: Android 4.2
Application Name: DynamicSpinner
Package Name: com.example.DynamicSpinner
Activity Name: DynamicSpinnerActivity

  1. Open DynamicSpinnerActivity.java file and write following code there:

 

package com.example.dynamicspinner;

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.Button;
import android.widget.Spinner;

public class DynamicSpinnerActivity extends Activity {

Button btnMove;
Spinner MySpinner1, MySpinner2;
List<String> myList1, myList2;
private ArrayAdapter<String> myAdapter1, myAdapter2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnMove = (Button)findViewById(R.id.createspinner);
MySpinner1 = (Spinner)findViewById(R.id.spinner1);
MySpinner2 = (Spinner)findViewById(R.id.spinner2);

initList();
myAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, myList1);
myAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MySpinner1.setAdapter(myAdapter1);

myAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, myList2);
myAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MySpinner2.setAdapter(myAdapter2);

btnMove.setOnClickListener(MoveOnClickListener);
}

Button.OnClickListener MoveOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int pos = MySpinner1.getSelectedItemPosition();

if(pos != AdapterView.INVALID_POSITION){
myList2.add(myList1.get(pos));
myList1.remove(pos);
myAdapter1.notifyDataSetChanged();
myAdapter2.notifyDataSetChanged();
}
}};

void initList(){
myList1 = new ArrayList<String>();
myList1.add("January");
myList1.add("February");
myList1.add("March");
myList1.add("April");
myList1.add("May");
myList1.add("June");
myList1.add("July");
myList1.add("August");
myList1.add("September");
myList1.add("Octomber");
myList1.add("November");
myList1.add("December");

myList2 = new ArrayList<String>();
}
}

 

  1. Compile and build the project.

Leave a Comment: