Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spinner dropdown view not transparent

I am using a custom Spinner with TextView. I set the dropdown layout background to transparent.But when the app runs only the Spinner is transparent.The dropdown view shows white color.

image:

enter image description here

main_activity.xml:

<Spinner
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:gravity="center"
        android:layout_below="@+id/phone"
        android:spinnerMode="dropdown"
        android:id="@+id/eventspinner"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
/>

spinner_dropdown_layout.xml:

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

    <TextView
        android:id="@+id/eventText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginLeft="10dp"
        android:text="Text Here"
        android:background="@android:color/transparent"
        android:layout_centerVertical="true">
    </TextView>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginTop="14dp"
        android:layout_below="@+id/eventText"
        android:background="#90000000"></View>
</RelativeLayout>

CustomAdapter.java:

public class CustomAdapter extends ArrayAdapter<String> {
    public CustomAdapter(Context context, int resource, List objects) {
        super(context, resource, objects);
    }
    public View getCustomView(int position,View convertview,ViewGroup parent){
        LayoutInflater layoutInflater=getLayoutInflater();
        View view= layoutInflater.inflate(R.layout.spinner_dropdown_layout,parent,false);
        view.setBackgroundColor(Color.TRANSPARENT);
        TextView eventText=(TextView) view.findViewById(R.id.eventText);
        eventText.setText((CharSequence) spinnerData.get(position));
        return view;


    }
    @Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }
}

MainActivity.java:

 spinnerData=new ArrayList();
            spinnerData.add("Select from below");
            spinnerData.add("Marriage");
            spinnerData.add("House Warming");
            eventSpinner=(Spinner) findViewById(R.id.eventspinner);
            eventSpinner.setAdapter(new CustomAdapter(this,R.layout.spinner_dropdown_layout,spinnerData));
            eventSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Snackbar.make(main_layout, (CharSequence) parent.getItemAtPosition(position),Snackbar.LENGTH_SHORT).show();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                Snackbar.make(main_layout,"Noting selected",Snackbar.LENGTH_SHORT).show();
                }
            });

color.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#ffffff</color>
</resources>
like image 437
jobin Avatar asked Aug 30 '25 18:08

jobin


2 Answers

You need to set dropdown background color.

ex-

android:popupBackground="#COLOR_CODE"

<Spinner
  android:layout_width="match_parent"
  android:layout_height="35dp"
  android:gravity="center"
  android:popupBackground="@android:color/transparent"
  android:layout_below="@+id/phone"
  android:spinnerMode="dropdown"
  android:id="@+id/eventspinner"
  android:layout_marginTop="10dp"
  android:layout_marginLeft="30dp"
  android:layout_marginRight="30dp"
/>
like image 55
Tushar Patil Avatar answered Sep 02 '25 09:09

Tushar Patil


Add transparent background to spinner

<Spinner
    android:background="@android:color/transparent"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:gravity="center"
    android:layout_below="@+id/phone"
    android:spinnerMode="dropdown"
    android:id="@+id/eventspinner"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
/>
like image 24
Praveen Avatar answered Sep 02 '25 08:09

Praveen