Sunday 22 December 2013

how to Implement two spinners in one activity in android

Today i am going to explain you how to implement two spinners in one activity. many of the android application developers gets a situation in which they have to implement two spinners on one module. generally they are required when you want to take a query from the user. By the use of the spinner you can make your application more attractive and it is also very user friendly because user can choose the option from that he don't have to write anything.

So lets start -

First steps : - create a drawable folder in the res directory and create a XML file in it lets say row.XML
                    like this ....

<?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="wrap_content"
android:orientation="horizontal"
>

<TextView
    android:id="@+id/value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:text="Interested in"
    android:textColor="#990000"
    android:textColorLink="#990000"
    android:textSize="25sp" />

</LinearLayout>

Step Two:- Go to your layout folder and add two spinners in that and two text view. we are taking text view to show the value which is selected by the user.

Main XML Code:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/spinner1" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="click"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/result"
        android:layout_marginTop="51dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="48dp" />

</RelativeLayout>

Step Three :- Now we will do some coding. first initialize the spinner after that  set an string arrayadapter to take the reference of row.XML file and a string array. after that set adapter on the spinner and adapter is your arrayadapter and at last set OnItemSelectedListener on the spiner. your code will be look like this.

        spinner1=(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        R.drawable.row, R.id.value, Dinner);
        spinner1.setAdapter(adapter);
        spinner1.setOnItemSelectedListener(this);

Full Source Code :-

public class MainActivity extends Activity implements OnItemSelectedListener{

    Spinner spinner1,spinner2;
    String Dinner [] = {"Light","Normal","Heavy"};
    String inter [] = {"None","Yes"};
    String diner,option;
    Button result;
    TextView t1,t2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        result=(Button)findViewById(R.id.result);
        t1=(TextView)findViewById(R.id.textView1);
        t2=(TextView)findViewById(R.id.textView2);
               
        spinner1=(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        R.drawable.row, R.id.value, Dinner);
        spinner1.setAdapter(adapter);
        spinner1.setOnItemSelectedListener(this);
       
        spinner2=(Spinner)findViewById(R.id.spinner2);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
        R.drawable.row, R.id.value, inter);
        spinner2.setAdapter(adapter1);
        spinner2.setOnItemSelectedListener(this);
        /*
        result.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                t1.setText(diner)
            }
        });
        */
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        Spinner spin = (Spinner)arg0;
        Spinner spin2 = (Spinner)arg0;
       
        if(spin.getId() == R.id.spinner1)
        {
            diner=Dinner[arg2];
            t1.setText(diner.toString());
        }
        else if(spin2.getId() == R.id.spinner2)
        {
            option=inter[arg2];   
            t2.setText(option.toString());
        }
    }

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


snapshots:-





















Click here to download the project:-





1 comment: