Android spinner (drop down list) example


A Spinner is used to select a  value from a list.By default, the selected value is displayed on spinner.When we touch the spinner it will shows the all other available values,from which the user can select a new one. Spinner values can be set using ArrayAdapter or string array defined in a string resource file.

You can download source code here.

Create Android Project with name as SpinnerDemo  with package name "com.etr.drawshape". Create Activity with name as  DrawActivity. This is the code for DrawActivity.

package com.etr.drawshape;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class DrawActivity extends Activity {
TextView text;
Spinner spin;
ArrayList<String> value = new ArrayList<String>();
String selectedShape;

@Override
protected void onCreate(Bundle savedInstanceState) {
try {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);

value.add("Circle");
value.add("Line");
value.add("Oval");
value.add("Rectangle");

spin = (Spinner) findViewById(R.id.shapes);
text = (TextView) findViewById(R.id.shape_text);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, value);
spin.setAdapter(adapter);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selectedShape = value.get(arg2);
text.setText(value.get(arg2));
}

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

}

});

} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.draw, menu);
return true;
}

}

This is code for activity_draw.xml here we can add spinner xml element.

<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"
    android:orientation="vertical"
    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=".GraphicsActivity" >

    <Spinner
        android:id="@+id/shapes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/shpes_body"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/shape_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="select value" />
    </LinearLayout>

</LinearLayout>


Comments

Popular posts from this blog

SQLiteDatabase With Multiple Tables

Programmatically turn ON/OFF WiFi on Android device

Android Service and IntentService