Android spinner example

Spinner is an android widget which allows you to select an item from a drop down menu.In this tutorial we will learn how to implement a spinner in android. Let us start by creating a new project in Android studio.

1.Creating New Project

  1. Create a new project in Android Studio from File -> New Project
  2. Fill the required fields like Application name and package name and click Next
  3.  Select Basic activity and click Next, and click  Finish Button.A default activity will be created as MainActivity.java with layouts activity_main.xml and content_main.xml

2. following is the code for res/layout/activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="codes4.com.androidspinner.MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

3.  following is the code for res/layout/content_main.xml

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="codes4.com.androidspinner.MainActivity"
tools:showIn="@layout/activity_main">

<Spinner
android:id="@+id/sp_languages"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/spinner_title"/>

</RelativeLayout>

4. We need to have an ArrayAdapter which consist of array of strings to be displayed inside a spinner ,In the the following MainActivity.java code we will have the reference of spinner in above layout code and we will set the array adapter to spinner.

MainActivity.java


package codes4.com.androidspinner;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

Toolbar toolbar;
Spinner sp_languages;
ArrayList<String> languagesArrayList;
ArrayAdapter<String> languagesArrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//method for initialisation
init();
sp_languages.setAdapter(languagesArrayAdapter);
sp_languages.setOnItemSelectedListener(this);
}

private void init() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

sp_languages = (Spinner) findViewById(R.id.sp_languages);
languagesArrayList = new ArrayList<>();
languagesArrayList.add("sql");
languagesArrayList.add("java");
languagesArrayList.add("java script");
languagesArrayList.add("python");
languagesArrayList.add("c");
languagesArrayList.add("c++");
languagesArrayList.add("Android");

languagesArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,languagesArrayList);
languagesArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

String item = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(),item,Toast.LENGTH_SHORT).show();

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}

5. finally add the below code to your res/values/string.xml


<string name="spinner_title">Select language</string>

output

android spinner
android spinner
 

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

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