Style TextView like a Spinner with appcompat v21
Asked Answered
S

4

7

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" />
Syngamy answered 6/12, 2014 at 20:10 Comment(4)
Why don't you use a Spinner if you need a spinner?Eulalia
I need a spinner with custom dialog and this is the easiest way to do itSyngamy
Aha, so you're re-implementing android:spinnerMode="dialog".Eulalia
@Eulalia they made this stuff absolutely unusable in terms of loader manager lifecycle, so we re-implementing this stuff once again.Yardarm
E
18

I'd go with:

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

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

Eulalia answered 6/12, 2014 at 20:24 Comment(4)
This doesn't work for me on pre-Lollipop. Will now publish how I've done it.Milano
@androiddeveloper I tested on 4.4, but looking at the date this may have been before the overhauled appcompat-v7 for Lollipop.Eulalia
Makes sense. Sorry. if you find a better solution than what I did, please update and notify about it.Milano
Doing this in AndroidX gives the TextView a dropdown arrow.Hornbook
M
1

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.

Milano answered 2/7, 2015 at 9:59 Comment(6)
This method worked well for pre-lollipop devices. However, it didn't look good on lollipop because there was box drawn in the background. I had to use TextView with style="@android:style/Widget.AppCompat.Spinner" on API 21 or later. I used AppCompat v23.0.1.Efflux
@Efflux Hmmm... this was a long time ago, and it used to work. Will now add what's working for me.Milano
@androiddeveloper I tried your latest sol. of using Widget.AppCompat.Light.Spinner.DropDown.ActionBar and yes I tried on non-actionbar item. But on 4.1 device, it is not working, meaning does not show a drop-down spinner UI but only displays Plain TextView.Milkweed
@androiddeveloper No, It does not on device running pre-lollipop. Hence, I developed another solution and posted below: Just in case if anyone trying to use it for pre-lollipop devices.Milkweed
@AADTechnical I've tested it on an emulator of API 10 (GB), and it works (shows a small white triangle next to the text). Maybe you should change the background of the view that contains the TextView .Milano
@androiddeveloper OK, I will try your above trick to see if it can help.Milkweed
M
0

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"
     />
Milkweed answered 9/12, 2015 at 5:18 Comment(0)
H
0

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

Hornbook answered 12/4, 2020 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.