Wednesday, October 23, 2013

Spinner View ( Drop down list )



Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.

In Android, you can use “android.widget.Spinner” class to render a dropdown box selection list.

In this tutorial we show you how to do the following tasks :

  1. Render a Spinner in XML, and load the selection items via XML file also.
  2. Render another Spinner in XML, and load the selection items via code dynamically.
  3. Attach a listener on Spinner, fire when user select a value in Spinner.
  4. Render and attach a listener on a normal button, fire when user click on it, and it will display selected value of Spinner.

File : res/values/strings.xml

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

    <string name="app_name">Spinner Drop down list</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    
    <string name="versions_prompt">Choose a Version</string>

    <string-array name="versions_arrays">
        <item>Cupcake</item>
        <item>Donut</item>
        <item>Eclairs</item>
        <item>Froyo</item>
        <item>Gingerbread</item>
        <item>Honeybee</item>
        <item>Icecream sandwich</item>
        <item>Jellybean</item>
        <item>Kitkat</item>
    </string-array>

</resources>



File : res/layout/activity_spinner.xml

<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:padding="20dp"
    tools:context=".SpinnerActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:entries="@array/versions_arrays"
        android:prompt="@string/versions_prompt" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner1"
        android:layout_below="@+id/spinner1"
        android:layout_marginTop="20dp" />

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner2"
        android:layout_below="@+id/spinner2"
        android:layout_marginTop="20dp"
        android:text="Submit" />

</RelativeLayout>


File : SpinnerActivity.java

package com.mindmedia.spinner;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class SpinnerActivity extends Activity {

private Spinner spinner1, spinner2;
private Button btnSubmit;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}

// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("Android");
list.add("Iphone");
list.add("Blackberry");
list.add("Windows");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}

public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}

// get the selected dropdown list value
public void addListenerOnButton() {

spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);

btnSubmit = (Button) findViewById(R.id.btnSubmit);

btnSubmit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Toast.makeText(
SpinnerActivity.this,
"Spinner 1 : "
+ String.valueOf(spinner1.getSelectedItem())
+ "\nSpinner 2 : "
+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
}

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(parent.getContext(),
"Version : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}


Output :



















0 comments:

Post a Comment

    Blogger news

    MIND MEDIA INNOVATIONS PVT LTD
    25/2953(2), Old GPO Building,
    Near Ayurveda College,
    Kunnumpuram,
    Thiruvananthapuram-695001
    Phone: +91 471 257 1001

    Email us @ :
    mindmediateam@gmail.com

    Total Pageviews

    Share us

    Cool Social Media Sharing Touch Me Widget by Blogger Widgets

    About

    Our hands with you, towards Android Technology