Android Spinner drop down list example tutorial

How to create simple drop down list in android using spinner.

Spinner tag is basically used for create drop down list in android applications because there are no pre define tag is available for design drop down list menus. Spinner is applied with array list adapter because the list item is defined as dynamically using array adapter. So here is the complete step by step tutorial for Android Spinner drop down list example tutorial.

android-project-download-code-button

Android Spinner drop down list example tutorial.

Code for MainActivity.java file.

package com.android_examples.com.spinnerandroidexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

 Spinner spinnerView;
 String[] spinnerValue = {"M.C.A", "B.C.A", "PHD", "B.TECH"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 spinnerView =(Spinner)findViewById(R.id.spinner1);
 ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, spinnerValue);
 spinnerView.setAdapter(adapter);
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout 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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.spinnerandroidexample.MainActivity" >

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="77dp" />

</RelativeLayout>

Screenshot:

Android Spinner drop down list example tutorial

Click Here To Download Android Spinner drop down list example tutorial project.

Leave a Reply

Your email address will not be published. Required fields are marked *