Android Spinners Dropdownlist Example Tutorial

Android Spinners Dropdownlist Example Tutorial

In Android it’s extremely simple to generate Drop Down Lists with the use of Spinner feature. In this tutorial, you will learn how to set up and show a Spinner.

We are going to learn how to create a Spinner Dropdownlist in our android application by using string-array resource in xml form.

Note that the same method can also be done through the use of Adapter that will populate the Spinner in run-time.

Before we start, the first thing I will do is to list the environment and tools I used in this android code snippet but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidSpinnersDropdownlist

Package: com.inducesmile.androidspinnersdropdownlist

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, make sure you change the package name if you did not use the same package.

Incorporating resources

We are going to create our string-array resource. Open the strings.xml file located in the following path res/values/strings.xml

When you open the strings.xml file, you are free to use the code or design view for your design.

The design view is simple tool you can use to add a lot of resources to your application such as strings, integers, and color values and so on.

But we are going to make use of the standard way which is editing the strings.xml file by hand. In the bottom of the screen, hit the string.xml tab.

Copy and paste the following code in your strings.xml file.

<resources>

<string name="app_name">AndroidSpinnersDropdownlist</string>

<string name="hello_world">Hello world!</string>

<string name="action_settings">Settings</string>

<string name="select">Choose an object</string>

<string-array name="object_array">

<item>House</item>

<item>Car</item>

<item>Cake</item>

<item>Money</item>

<item>Food</item>

<item>Phone</item>

<item>Laptop</item>

</string-array>

</resources>

Thus we’ve merely generated a few string resources that we can make use of in a lot of ways and in a lot of places in our app. We’ve as well produced a string-array resource. This is a very significant feature when you want to group together a collection of strings that you wish to make use of as a list in your application.

In this instance, we will load one of the spinners with this specific array of strings, and it will routinely create the particular options on the drop down list.

One of the major advantage of using this method is that offers the freedom to change the constitute of the string array without affecting the code.

Spinner User Interface

Open res/layout/activity_main.xml file:

Copy and paste the following code:

<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:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<Spinner

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/spinner"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:entries="@array/object-array"

android:prompt="@string/option_select"

android:layout_marginTop="30dp" />

</RelativeLayout>

In the code above, observe the android:entries=”@array/object_array” attribute. This tells the Spinner to make use of a specific array resource (in this instance the string-array we defined earlier) in order to produce the options on the drop down list.

The spinner attribute called prompt is use to create the header information for the spinner. This is important because it let users to understanding what it does.

Copy and paste the following code in MainActivty.java file

package inducesmile.com.androidspinnersdropdownlist;

import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Spinner spinner = (Spinner) findViewById(R.id.spinner);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.object_array, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

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

Toast.makeText(getApplicationContext(), "Item number: " + position, Toast.LENGTH_LONG).show();

}

});

}

@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);

}

}

Save the file and run your project. If everything works for you, an image like this will appear on your test device.

Android Spinners Dropdownlist Example Tutorial

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published

OTHER INTERESTING POSTS:

No Responses

Add a Comment