Monday, 4 June 2012

Simple Spinner Demo


/* This is Simple Spinner Demo of Android */

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" >


    <Spinner
        android:id="@+id/spnDemo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>


 SpinnerDemoActivity.java



package com.spinnerdemo;

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.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class SpinnerDemoActivity extends Activity {
    /** Called when the activity is first created. */
Spinner spnDemo;
String[] data = {"Selet Item","Item 1"};
ArrayAdapter<String> spnAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spnAdapter= new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,data);
        spnAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnDemo=(Spinner)findViewById(R.id.spnDemo);
        spnDemo.setPrompt("My List");
        spnDemo.setAdapter(spnAdapter);
     
        spnDemo.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(arg2!=0)
Toast.makeText(SpinnerDemoActivity.this, arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_SHORT).show();
}

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

}
});
    }
}




0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Web Host