TextView style as Spinner with appcompat v21

I want the TextView to look like a spinner with a new Material style.

I managed to do this using the Widget.Material.Light.Spinner style, but I did not find an alternative in AppCompat resources (v21). My xml:

<TextView android:id="@+id/sp_league_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" tools:text="Premier league" style="@android:style/Widget.Material.Light.Spinner" android:textColor="@android:color/white" /> 
+7
android android-textview android-spinner material-design appcompat
source share
3 answers

I would go with:

 style="@style/Widget.AppCompat.Spinner" 

But feel free to choose another: enter image description here

+13
source share

The style solution does not work for me, but I found another solution:

Instead, I use AppCompatButton and have this:

XML:

 <android.support.v7.widget.AppCompatButton android:background="@drawable/abc_spinner_mtrl_am_alpha" ... /> 

Java:

 ((AppCompatButton)findViewById(...)).setSupportBackgroundTintList(new ColorStateList(new int[][]{new int[0]}, new int[]{0xff52A1E8})); 

EDIT: It seems like it no longer works, but found another solution:

 <style name="Widget.MyTheme.HeaderBar.Spinner" parent="Widget.AppCompat.Light.Spinner.DropDown.ActionBar"> </style> 

It’s true that this is for the action bar, but it should also work for other cases.

+1
source share

I tested the solutions mentioned here

1.Using style="@style/Widget.AppCompat.Spinner ) and
Widget.AppCompat.Light.Spinner.DropDown.ActionBar

Both of these actions work only on Android 5.x and higher devices and do not work for Android devices running under 4.x and lower.

Therefore, I am posting my solution for those who want to have the β€œDrop-down” effect for TextView for all devices.

An application needs to create a Drawable folder inside Drawable in the application, say dropdown_spinner.xml

 <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/ic_icon_arrow" android:gravity="end" /> 

And then just use this as the background for the TextView, as shown below:

 <TextView android:id="@+id/mySpinnerTextView" android:background="@drawable/dropdown_spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> 
0
source share

All Articles