Pages

Saturday, September 8, 2012

Android Spinner Example



package sample.spin;

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

public class SpinnerEx extends Activity implements OnItemSelectedListener {
 
String[] listcontent={"c","java","php","c++","jsp","c#","servlet","struts"

,"c","java","php","c++","jsp","c#","servlet","struts"};

TextView tv;

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.text);
        Spinner spin=(Spinner)findViewById(R.id.spin_id);
        spin.setOnItemSelectedListener( this);
 ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,listcontent);

    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(aa);
    }



public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub

tv.setText(listcontent[position]);

}



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

tv.setText("");
}
}


main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 
    xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
   
    android:id="@+id/text"
    />
    <Spinner
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spin_id"
    />
</LinearLayout>


No comments:

Post a Comment