1

I have a background image set for my spinner, but when I select an item from the drop-down list of the spinner, my spinner background image is replaced by the selected item, I want my background image to remain there and I don't want the selected item to be shown instead of the background image of spinner. How do I do that?

Code for my custom adapter:

public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
        super(ctx, txtViewResourceId, objects);
    }

    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
        return getCustomView(position, cnvtView, prnt);

    }
    @Override
    public View getView(int pos, View cnvtView, ViewGroup prnt) {
        return getCustomView(pos, cnvtView, prnt);
    }
    public View getCustomView(int position, View convertView,
            ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,
                false);
        TextView main_text = (TextView) mySpinner
                .findViewById(R.id.text_main_seen);
        main_text.setText(spinnerValues[position]);





        return mySpinner;
    }
}

Code for my custom spinner xml

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

    <TextView
        android:id="@+id/text_main_seen"
        android:layout_width="fill_parent"
        android:layout_height="30sp"
        android:background="#008000"
        android:gravity="center"

        android:textColor="#ffffff"
        android:textSize="30px" />

</RelativeLayout>

Code for my actual spinner.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/planets_spinner"
        android:layout_width="100dp"
        android:layout_height="50sp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="#008000"
        android:layout_weight="0.08"
        android:drawSelectorOnTop="true" />

</RelativeLayout

and code of my mainActivity where i am calling the spinner is

Spinner mySpinner = (Spinner) findViewById(R.id.planets_spinner);
    mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner,
            spinnerValues));
gunar
  • 14,660
  • 7
  • 56
  • 87
Hassaan Rabbani
  • 2,469
  • 5
  • 30
  • 55
  • could you post the code? – MikeKeepsOnShine Oct 11 '13 at 14:15
  • This question seem duplicated, please look theses posts [`Change Spinner Selector Item`](http://stackoverflow.com/questions/7584158/setting-background-color-for-spinner-item-on-selection) and [`Change Spinner background`](http://stackoverflow.com/questions/15379851/change-text-color-of-selected-item-in-spinner) – Jans Oct 11 '13 at 14:23

1 Answers1

0

I wouldn't use a spinner at all for this. Just use a Button made to look like a Spinner or whatever else you want it to look like. You can make a regular Button look like a Spinner with this attribute:

android:background="@android:drawable/btn_dropdown"

Or, make your own background that looks like a Spinner and use that.

Then, have the onClick event of your Button open up a "single-item-select" Dialog.

SBerg413
  • 14,515
  • 6
  • 62
  • 88