Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

"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);

If you have included android support library version 18 or above into your project then you can replace the resource id simple_spinner_dropdown_item with support_simple_spinner_dropdown_item. That will remove the radio button.

use simple_dropdown_item_1line

The "cleanest" way of doing this (just remove the check mark and not touch anythinbg else including text style, item size etc.) is to create custom adapter (like in the answers above):

public class SimpleSpinnerArrayAdapter extends ArrayAdapter<String> {

    public SimpleSpinnerArrayAdapter(Context context, String[] data) {
        super(context, android.R.layout.simple_spinner_item, data);
        this.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    public SimpleSpinnerArrayAdapter(Context context, List<String> data) {
        super(context, android.R.layout.simple_spinner_item, data);
        this.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    /**
     * Returns default dropdown view with removed checkbox
     */
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view = super.getDropDownView(position, convertView, parent);
        if (view != null && view instanceof CheckedTextView) {
            ((CheckedTextView) view).setCheckMarkDrawable(null);
        }
        return view;
    }
}

Please notice the getDropDownView() method which returns the view for dropdown list item. You can use any custom view here but if you would like to stick to the default view you should probably use the above code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top