Jan 6, 2014

Custom Spinner with OnItemSelectedListener

Example to implement custom Spinner with OnItemSelectedListener.

Custom Spinner with OnItemSelectedListener
Custom Spinner with OnItemSelectedListener

package com.example.androidcustomspinner;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 private class MyObject {
  private int number;
  private String name;

  MyObject(int num, String nam) {
   number = num;
   name = nam;
  }

  public int getNumber() {
   return number;
  }

  public String getName() {
   return name;
  }
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Spinner spinner = (Spinner) findViewById(R.id.spinner);

  // Init ArrayList of MyObject
  ArrayList<MyObject> myArrayList = new ArrayList<MyObject>();
  myArrayList.add(new MyObject(0, "Sunday"));
  myArrayList.add(new MyObject(1, "Monday"));
  myArrayList.add(new MyObject(2, "Tuesday"));
  myArrayList.add(new MyObject(3, "Wednesday"));
  myArrayList.add(new MyObject(4, "Thursday"));
  myArrayList.add(new MyObject(5, "Friday"));
  myArrayList.add(new MyObject(6, "Saturday"));

  MyAdapter myAdapter = new MyAdapter(this, myArrayList);
  spinner.setAdapter(myAdapter);
  
  spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

   @Override
   public void onItemSelected(AdapterView<?> parent, View view, 
     int position, long id) {
    MyObject clickedObj = (MyObject)parent.getItemAtPosition(position);
       Toast.makeText(MainActivity.this, 
         "Clicked item:\n" +
         clickedObj.getNumber() + ": " +
         clickedObj.getName(), 
         Toast.LENGTH_LONG).show();
   }

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

 }

 private class MyAdapter extends BaseAdapter {

  private ArrayList<MyObject> myList;

  private Activity parentActivity;
  private LayoutInflater inflater;

  public MyAdapter(Activity parent, ArrayList<MyObject> l) {
   parentActivity = parent;
   myList = l;
   inflater = (LayoutInflater) parentActivity
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }

  @Override
  public int getCount() {
   return myList.size();
  }

  @Override
  public Object getItem(int position) {
   return myList.get(position);
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   View view = convertView;
   if (convertView == null)
    view = inflater.inflate(R.layout.row, null);

   TextView text1 = (TextView) view.findViewById(R.id.text1);
   TextView text2 = (TextView) view.findViewById(R.id.text2);
   MyObject myObj = myList.get(position);
   text1.setText(String.valueOf(myObj.getNumber()));
   text2.setText(myObj.getName());
   return view;
  }

 }
}

row.xml, define the layout of individual row in the custom Spinner.
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >
 
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"/>
 
</LinearLayout>

Main layout
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
</LinearLayout>

Also read: Implement auto scroll marquee TextView in Spinner

No comments:

Post a Comment

Infolinks In Text Ads