Spinner Application

Spinner is same as ComboBox Control. In android we called combobox as spinner. Traditionally, it is a combination of a drop-down list or list box, allowing the user to select a value from the list. Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value.

Step 1.

Create new application with any name from file menu option.
File → New Project → Application name → select API → Add Blank Activity → Activity Name → finish

Step 2.

In activity_main.xml layout file design your application.
Add following Controls:-

spinner

Step 3.

Now you have to modify your activity_main.xml file as shown in below section.


<LinearLayout 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" tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select an Item:"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" android:textSize="50px"/>
<LinearLayout android:orientation="vertical"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:weightSum="100">
    <Spinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_marginTop="10dp"
        android:layout_weight="100"/>
</LinearLayout>
</LinearLayout>

    

Step 4.

Import Package and write following coding in MainActivity.java class file.

	
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.Spinner;
    import android.widget.ArrayAdapter;
    import android.view.View;
    import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnItemSelectedListener
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner spinner=(Spinner)findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(this);

        // Spinner Drop down elements
        String[] items={"Automobile","Business Services","Computers","Education","Personal","Travel"};

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
    }

    @Override

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
    }
    
    

Step 5. Final Output

This is the final output of the Application. In MainActivity.java file in last we have used Toast concept, which give message on selecting any option for few second.

Advertisement here

Share this Page.