Tuesday, March 20, 2012

Spinner Widget

Spinner-Widget 



 dasdd
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 HelloSpinnerActivity extends Activity implements OnItemSelectedListener{
    /** Called when the activity is first created. */
    Spinner sp;
    ArrayAdapter<String> ad;
    String str[]={"Ajumal","Rahul","Binnesh"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sp=(Spinner)findViewById(R.id.spinner);
        ad=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,str);
        ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(ad);
        sp.setOnItemSelectedListener(this);

        }
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(this,str[arg2], Toast.LENGTH_LONG).show();
       }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub       
    }
}

--------------------------------------------------------------------------------------------------

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
       
    />
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
    />

</LinearLayout>

No comments :

Post a Comment