Vasilisfoo Vasilisfoo - 4 months ago 39
Android Question

How to remove the Shadow of the Spinner DropDownView

I use the widget

Spinner
with a custom adapter and custom views in
spinnerMode
dropdown. My problem is that I cannot remove the default shadow that casts the dropdown of the
Spinner
.
enter image description here

Here is the code inside the
Activity


mSpinner = (Spinner)findViewById(R.id.spinner);
setSpinnerData();

customSpinnerAdapter = new CustomSpinnerAdapter(this,R.layout.spinner_item,CustomListViewValuesArr );
mSpinner.setAdapter(customSpinnerAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
itemSelect(pos);
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});


Also this is the Spinner inside the
Layout
of the
Activity
:

<Spinner
android:id="@+id/spinner"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:spinnerMode="dropdown"
android:background="@null"
android:padding="0dp"
android:layout_margin="15dp"
android:dropDownVerticalOffset="@dimen/vertical_offset"/>


I use my own custom
Adapter
:

public class CustomSpinnerAdapter extends ArrayAdapter {
public ArrayList CustomListViewValuesArr;
LayoutInflater inflater;
Typeface myTypeFace;
SpinnerItem item = null;

public CustomSpinnerAdapter(Context context, int textViewResourceId, ArrayList CustomListViewValuesArr) {
super(context, textViewResourceId, CustomListViewValuesArr);
this.CustomListViewValuesArr = CustomListViewValuesArr;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.myTypeFace = Typeface.createFromAsset(context.getAssets(),"fonts/OpenSans-Regular.ttf");
}

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

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

public View getCustomDropDownView(int position, View convertView, ViewGroup parent) {
Log.d("","CustomListViewValuesArr" + position);
/********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
View row = inflater.inflate(R.layout.simple_spinner_dropdown_item, parent, false);
item = (SpinnerItem) CustomListViewValuesArr.get(position);
/***** Get each Model object from Arraylist ********/
TextView label = (TextView)row.findViewById(R.id.spinner_drop_down);
// Default selected Spinner item
label.setText(item.getFilterName());
label.setTypeface(myTypeFace);
return row;
}

public View getCustomView(int position, View convertView, ViewGroup parent) {
Log.d("","CustomListViewValuesArr" + position);
/********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
View row = inflater.inflate(R.layout.spinner_item, parent, false);
item = (SpinnerItem) CustomListViewValuesArr.get(position);
/***** Get each Model object from Arraylist ********/
TextView label = (TextView)row.findViewById(R.id.spinner_item);
TextView hint = (TextView)row.findViewById(R.id.spinner_hint);
hint.setTypeface(myTypeFace);
// Default selected Spinner item
label.setText(item.getFilterName());
label.setTypeface(myTypeFace);
return row;
}
}


And finally I use custom views for the Dropdown

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:elevation="-6dp"
android:layout_height="35dp">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/spinnerDropDownItemStyle"
android:singleLine="true"
android:id="@+id/spinner_drop_down"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="@color/spinner_font_color"
android:layout_marginLeft="20dp"
android:textSize="14sp"
android:layout_marginStart="20dp"
android:ellipsize="marquee">
</TextView >

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_hint_change_pass"/>




and for the
Spinner
Item

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:ellipsize="marquee"
android:background="@drawable/spinners_background"
style="@style/spinnerItemStyle"
android:layout_height="35dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/spinner_hint"
android:gravity="center"
android:textStyle="bold"
android:textColor="@color/spinner_font_color"
android:textSize="14sp"
android:id="@+id/spinner_hint"
android:layout_marginRight="5dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_alignTop="@+id/spinner_item" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner_item"
android:textColor="@color/spinner_header_color"
android:gravity="center"
android:text="sss"
android:textSize="14sp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/spinner_hint"
android:layout_toEndOf="@+id/spinner_hint" />

<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/down_arrow_spinner"
android:layout_marginRight="17dp"
android:layout_marginEnd="1dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/imageView2" />




What I have tried and didn't worked :


  • Add Elevation to 0dp in the
    Spinner
    widget

  • Add null background to Spinner



Thanks in Advance !

Answer

Eventually I used an attributed of a spinner android:popupBackground="@null" with this the background of the pop up of the spinner is removed so and the shadow does. The only thing the I had to change was to Assign a background of #fff in my custom spinner dropdown item. So I changed it like this:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="35dp">
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/spinnerDropDownItemStyle"
        android:singleLine="true"
        android:id="@+id/spinner_drop_down"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:textColor="@color/spinner_font_color"
        android:layout_marginLeft="20dp"
        android:textSize="14sp"
        android:layout_marginStart="20dp"
        android:ellipsize="marquee">
    </TextView >

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/color_hint_change_pass"/>
</RelativeLayout >