Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spinner items as a grid

By default when Spinner is clicked, items are showed as list. I want to change the items showed as grid. How can I do that? I just need some direction.

Thanks UPDATE: This is my code. My spinner drop down is showing up as multiple rows of grid.I have attached a picture of how it looks like at this moment. I am also unable to select the values.

enter image description here

    package com.example.com.android.spinner;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class AndroidCustomSpinner extends Activity {

    String[] DayOfWeek = {"Sunday", "Monday", "Tuesday", 
            "Wednesday", "Thursday", "Friday", "Saturday"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
        mySpinner.setAdapter(new MyCustomAdapter(AndroidCustomSpinner.this, R.layout.row, DayOfWeek)); 
    }

    public class MyCustomAdapter extends ArrayAdapter<String>{

        private Context context;

        public MyCustomAdapter(Context context, int textViewResourceId,
                String[] objects) {
            super(context, textViewResourceId, objects);
            this.context=context;

            // TODO Auto-generated constructor stub
        }

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            TextView label=(TextView)convertView;

              if (convertView==null) {
                convertView=new TextView(context);
                label=(TextView)convertView;
              }

              label.setText(DayOfWeek[position]);

              return(convertView);

        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            //return super.getView(position, convertView, parent);

            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.row, parent, false);
            GridView label=(GridView)row.findViewById(R.id.gridView1);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(AndroidCustomSpinner.this,android.R.layout.simple_list_item_1,DayOfWeek);
            label.setAdapter(adapter);


             return row;
        }   
    }

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">

<GridView 
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="50dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</GridView>
</LinearLayout>
like image 480
vikas sharma Avatar asked Oct 02 '22 10:10

vikas sharma


1 Answers

You need to override getDropDownView method of your Spinner to show the GridView.

And in order to select values out from your Spinner, use OnItemClickListener on your GridView to get callbacks. for example:

label.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
        Toast.makeText(getContext(), "" + pos, Toast.LENGTH_SHORT).show();
    }
});
like image 179
waqaslam Avatar answered Oct 07 '22 20:10

waqaslam