Sunday, April 13, 2014

Custom Spinner with different normal view and drop-down view

It's another example of custom Spinner, which have different normal view and drop-down view, using custom object and adapter for the spinner. Our custom object have two String, one for display text, another for Internet address.

In normal view, returned by getView() of our custom ArrayAdapter, there are one TextView to show the display text, and another button to open the target address if clicked. In drop-down view, returned by getDropDownView() of our custom ArrayAdapter, there are two TextView to show the display text, and the target address.


/res/layout/row.xml, the layout of the normal view.
<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="wrap_content"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/gotext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"/>
    <Button
        android:id="@+id/gobutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

/res/layout/dropdown.xml, the layout of the drop-down view.
<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="wrap_content"
    android:orientation="vertical"
    android:padding="10dp" >
    
    <TextView 
        android:id="@+id/gotext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold" />
    <TextView 
        android:id="@+id/goaddr"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="italic" />
</LinearLayout>

/res/layout/activity_main.xml, the layout of our activity.
<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="com.example.androidspinnertext.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <Spinner
        android:id="@+id/spinnergo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textgo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
</LinearLayout>

MainActivity.java
package com.example.androidspinnertext;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

 MyClass[] objGo ={
  new MyClass("Android-er", "http://android-er.blogspot.com/"),
  new MyClass("Arduino-er", "http://arduino-er.blogspot.com/"),
  new MyClass("Hello Raspberry Pi", "http://helloraspberrypi.blogspot.com/"),
  new MyClass("MyPhotoBlog", "http://photo-er.blogspot.com/"),
  new MyClass("g+ Androider+", "https://plus.google.com/102969667192015169220"),
  new MyClass("Youtube playlist: Android Development", "http://www.youtube.com/playlist?list=PLP7qPet500deChwUlhq-GsDl8Tun4_WMD"),
  new MyClass("Google Play", "https://play.google.com/store")
 };
 
 Spinner spinnerGo;
 TextView textViewGo;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  textViewGo = (TextView)findViewById(R.id.textgo);
  spinnerGo = (Spinner)findViewById(R.id.spinnergo);
  MySpinnerAdapter adapterGo = 
   new MySpinnerAdapter(MainActivity.this, 
     R.layout.row, 
     objGo);
  spinnerGo.setAdapter(adapterGo);
  //spinnerGo.setOnItemSelectedListener(onItemSelectedListenerGo);

 }
 
 OnItemSelectedListener onItemSelectedListenerGo =
  new OnItemSelectedListener(){

   @Override
   public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
    MyClass obj = (MyClass)(parent.getItemAtPosition(position));
    textViewGo.setText(String.valueOf(obj.getTarget()));
   }

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

 };
 
 //define our custom class
 public class MyClass{

  private String text;
     private String target;

     public MyClass(String text, String target){
      this.text = text;
      this.target = target;
     }
     
     public void setText(String text){
         this.text = text;
     }

     public String getText(){
         return this.text;
     }

     public void setValue(String target){
         this.target = target;
     }

     public String getTarget(){
         return this.target;
     }
 }
 
 //custom adapter
 public class MySpinnerAdapter extends ArrayAdapter<MyClass>{

     private MyClass[] myObjs;

     public MySpinnerAdapter(Context context, int textViewResourceId,
       MyClass[] myObjs) {
         super(context, textViewResourceId, myObjs);
         this.myObjs = myObjs;
     }

     public int getCount(){
        return myObjs.length;
     }

     public MyClass getItem(int position){
        return myObjs[position];
     }

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

     @Override
     public View getView(final int position, View convertView, ViewGroup parent) {
      LayoutInflater inflater = getLayoutInflater(); 
      View spView = inflater.inflate(R.layout.row, parent, false);
      
      TextView sp_GoText = (TextView)spView.findViewById(R.id.gotext);
      sp_GoText.setText(myObjs[position].getText());
      
      Button sp_GoButton = (Button)spView.findViewById(R.id.gobutton);
      sp_GoButton.setText(myObjs[position].getTarget());
      
      sp_GoButton.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
     Uri uri = Uri.parse(myObjs[position].getTarget());
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     startActivity(intent);
    }});
      
      return spView;
     }

     @Override
     public View getDropDownView(int position, View convertView,
             ViewGroup parent) {
      LayoutInflater inflater = getLayoutInflater(); 
      View dropDownView = inflater.inflate(R.layout.dropdown, parent, false);
      
      TextView dd_GoText = (TextView)dropDownView.findViewById(R.id.gotext);
      dd_GoText.setText(myObjs[position].getText());
      
      TextView dd_GoAddr = (TextView)dropDownView.findViewById(R.id.goaddr);
      dd_GoAddr.setText(myObjs[position].getTarget());
      
      return dropDownView;
          
     }
     
 }

}



download filesDownload the files.

No comments:

Post a Comment