simple spinner android

Simple Spinner in android example

Simple Spinner in android example

If we want to show data in drop-down list we must use Spinner widget in android.Here we are going to learn how to make a simple spinner in android.To create this program we take an activity class named "SimpleSpinnerActivity.java"  and respecting xml file named  "activity_simple_spinner.xml" and also take a helper class named "SimpleSpinnerCollection.java" for collection of data.

Now we check the source code of "SimpleSpinnerActivity.java" class.


package com.androidfromhome.simplespinner;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SimpleSpinnerActivity extends Activity {

    private Spinner sp_android;
    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_spinner);
        SimpleSpinnerCollection.simple_arr_str=new ArrayList<String>();
        
        //add data to collection ArrayList
        SimpleSpinnerCollection.simple_arr_str.add("One");
        SimpleSpinnerCollection.simple_arr_str.add("Two");
        SimpleSpinnerCollection.simple_arr_str.add("Three");
        SimpleSpinnerCollection.simple_arr_str.add("Four");
        SimpleSpinnerCollection.simple_arr_str.add("Five");
        SimpleSpinnerCollection.simple_arr_str.add("Six");
        SimpleSpinnerCollection.simple_arr_str.add("Seven");
        SimpleSpinnerCollection.simple_arr_str.add("Eight");
        
        tv_result=(TextView)findViewById(R.id.tv_result);

        getSpinner();
    }

    public void getSpinner(){
        sp_android=(Spinner)findViewById(R.id.sp_android);
        if (SimpleSpinnerCollection.simple_arr_str!=null) {
            
            
            
        if (SimpleSpinnerCollection.simple_arr_str.size()>0) {
            
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SimpleSpinnerActivity.this, android.R.layout.simple_spinner_item,SimpleSpinnerCollection.simple_arr_str);
          
         dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          
sp_android.setAdapter(dataAdapter);

sp_android.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    

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

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        String result=SimpleSpinnerCollection.simple_arr_str.get(position);
        
        tv_result.setText("You Have Selected "+result);
        
        
    }});

        }    
        }
            
        
    }
}


xml file "activity_simple_spinner.xml"


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidfromhome.simplespinner.SimpleSpinnerActivity" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />

    </LinearLayout>

    <Spinner
        android:id="@+id/sp_android"
        android:layout_width="match_parent"
        android:layout_height="61dp"
        android:layout_margin="5dp"
        android:background="@android:drawable/btn_dropdown" />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp" />

</LinearLayout>

 


And now the collection class "SimpleSpinnerCollection.java"


 

package com.androidfromhome.simplespinner;

import java.util.ArrayList;

public class SimpleSpinnerCollection {

    public static ArrayList<String> simple_arr_str=null;

}


Download Source Code

 

Related Post:-

custom spinner in android go to the link

Custom Alert Dialog in android with custom xml layout

ListView in android example

Custom Toast example in android example

 

 

Comments

Leave a Reply

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

Get Widget