Android Spinner Dropdown

Android Spinner Dropdown - Displaying a List of Items:-
Spinners provide a fast way to choose one value from a group (Items) . Within the default state, a spinners shows its currently selected value (Item). Touching the spinners displays a dropdown menu with all other available values (items) , from which the user can choose a new one.

Xml Files:-
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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#ff5500"
        android:padding="10dp"
        android:text="Spinner Dropdown"
        android:textColor="#fff"
        android:textSize="20dp" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:popupBackground="#eafbe5" />

</RelativeLayout>

Java Files:-
SpinnerActivity.java

package com.parthiv.spinnerexample;


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

public class SpinnerActivity extends Activity {
Spinner spinnerDropDown;
String[] name = { "Parthiv", "Pandian", "Ashok", "vignesh",
"Raguvaran", "Raj", "Siva", "Sabari", "vijay" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);

// Get reference of SpinnerView from layout/main_activity.xml
spinnerDropDown = (Spinner) findViewById(R.id.spinner1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, name);

spinnerDropDown.setAdapter(adapter);

spinnerDropDown.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// Get select item
int sid = spinnerDropDown.getSelectedItemPosition();
Toast.makeText(getBaseContext(),
"You have selected Name : " + name[sid],
Toast.LENGTH_SHORT).show();
}

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

Outputs:-

                         












Keep Updating Every Day...

No comments:

Post a Comment

back to top