Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spinner - How to remove radio buttons?

In Android 1.6, upon tapping a spinner (drop-down menu), radio buttons appear next to the spinner options. How do I remove those radio buttons so that just the option text remains?

like image 714
iamkoa Avatar asked Dec 12 '09 00:12

iamkoa


4 Answers

Just to remove the radio buttons, you don't need your own adapter class.

Create a dropdown_item.xml in layout

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee" />

then make the following call in the code.

arrayAdapter.setDropDownViewResource(R.layout.dropdown_item);

The default spinner dropdown item is a CheckedTextView which has the radio button. Here you replace it with a TextView.

like image 68
kimkunjj Avatar answered Nov 12 '22 18:11

kimkunjj


You can use the android layout

android.R.layout.simple_spinner_item 

instead of

android.R.layout.simple_spinner_dropdown_item

but I recommend @kimkunjj answer, it will give you the control of the layout.

like image 26
TouchBoarder Avatar answered Nov 12 '22 19:11

TouchBoarder


If you want to get rid of radio buttons on the spinner list you have to provide your own layout for row.
Take a look at the example below:


package com.ramps;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class MySpinner extends Activity {
    //data that will be used as a spinner options
    private static String data[] = {"one", "two", "three"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //main.xml file contains spinner
        setContentView(R.layout.main);
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        //create your own adapter
        MySpinnerAdapter adapter = new MySpinnerAdapter(this,R.layout.custom_spinner_row,R.id.text, data );
        //set your custom adapter 
        spinner.setAdapter( adapter );
    }


    private class MySpinnerAdapter extends ArrayAdapter{

        public MySpinnerAdapter(Context context, int resource,
                int textViewResourceId, String[] objects) {
            super(context, resource, textViewResourceId, objects);          
        }   

    }
}


The custom layout for spinner row is just a simple LinearLayout with one TextView element which id is "text" (android:id="@+id/text")

This is just simple example. If you need more fancy layout than just TextView you would probably have to override getView() method of MySpinnerAdapter.

like image 4
Ramps Avatar answered Nov 12 '22 17:11

Ramps


"android.R.layout.simple_spinner_item" does the job,

programmatically that's:

modeSpinner=new Spinner(layout.getContext());
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(layout.getContext(),     
    android.R.layout.simple_spinner_item, Arrays.asList(modes));
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
like image 1
Mr. Clankey Avatar answered Nov 12 '22 18:11

Mr. Clankey