Idob
Idob

Reputation: 1650

Style TextView like a Spinner with appcompat v21

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

I managed to do it with "Widget.Material.Light.Spinner" style, but I didn't find any alternative in AppCompat (v21) resources. 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" />

Upvotes: 7

Views: 10133

Answers (4)

tfad334
tfad334

Reputation: 598

In AndroidX, use style="@style/TextAppearance.AppCompat.Widget.TextView.SpinnerItem"

Upvotes: 0

AADProgramming
AADProgramming

Reputation: 6345

I Tested the the solutions mentioned here

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

Both these works only on Android 5.x And Above Devices and do not work well for Android devices running on 4.x and below.

Hence, I am posting my solution for those who wanted to have a Drop-down like effect to TextView for all devices.

App needs to create a Drawable inside drawable folder in App, let's 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 simply use this as a background for the TextView as below:

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

Upvotes: 0

android developer
android developer

Reputation: 115932

The style solution didn't work for me, but I've found another solution:

I use AppCompatButton instead, 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: seems it won't work well anymore, but found another solution :

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

It's true that it's for the action bar , but it should work for other cases too.

Upvotes: 1

TWiStErRob
TWiStErRob

Reputation: 46470

I'd go with:

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

But feel free to pick another one: enter image description here

Upvotes: 18

Related Questions