1

My android app already set popupbackground as drawable xml. However, the popup dialog still cannot show the color I set. How to settle this issue?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CountrySelectorActivity">


    <Spinner
        android:id="@+id/search_spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/black"
        android:popupBackground="@drawable/spinner_background"
        android:spinnerMode="dialog"
        />

    <Spinner
        android:id="@+id/search_spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@color/black"
        android:popupBackground="@drawable/spinner_background"
        android:spinnerMode="dialog"/>

</LinearLayout>

@drawable/spinner_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/green"/>

        </shape>
    </item>

</selector>

enter image description here

enter image description here

Spinner activity code https://stackoverflow.com/questions/51495271/android-kotlin-spinner-working-for-api-23-but-not-working-for-api-21

James Z
  • 12,209
  • 10
  • 24
  • 44
GPH
  • 1,111
  • 6
  • 29
  • 49
  • Can you add your android activity code. When you build the dialog. – Crammeur Jul 24 '18 at 12:49
  • I add the link of code at the bottom now. – GPH Jul 24 '18 at 12:58
  • Possible duplicate of [android kotlin spinner working for API 23, but not working for API 21](https://stackoverflow.com/questions/51495271/android-kotlin-spinner-working-for-api-23-but-not-working-for-api-21) – Crammeur Jul 24 '18 at 13:00

2 Answers2

1

Make a style using your custom spinner background drawable. Then add the style as an attribute to your spinner. Lastly, programmatically change the spinner popup background color in your activity or fragment. The following way worked for me:

<style name="SpinnerTheme" parent="android:Widget.DeviceDefault.Spinner">
    <item name="android:background">@drawable/spinner_background</item>
    <item name="android:padding">8dp</item>
    <item name="android:paddingTop">5dp</item>
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:paddingBottom">5dp</item>
    <item name="android:paddingRight">15dp</item>
</style>

Put this in your xml for each spinner (REMOVE android:popupBackground="@drawable/spinner_background" & android:spinnerMode="dialog"):

<Spinner
    android:id="@+id/search_spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:background="@color/black"
    style="@style/SpinnerTheme"/>

Then in your activity or fragment, programmatically set the popup background color:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                spinner.setPopupBackgroundResource(R.color.yourColor);
            }

Here is a link to example: https://www.oodlestechnologies.com/blogs/Custom-Spinner-In-Android

rdmc
  • 21
  • 5
  • In real device SM-G900F API 23, it is working well. But it is not working in SM-N9005 API 21. It shows the spinner but it did not show any options when I click the spinner. – GPH Jul 25 '18 at 06:12
  • If you want to support pre-Lollipop versions, you may need to try changing the style to have a parent with an appCompat theme, and make sure you include dependency for appCompat in your app level gradle file. Here is a link to google's blog post about appCompat: https://android-developers.googleblog.com/2014/10/appcompat-v21-material-design-for-pre.html – rdmc Jul 25 '18 at 11:26
1

With AndroidX and using AppCompatSpinner you can do this:

File: styles.xml

<style name="MyPopUpTheme">
    <item name="android:background">@color/background_spinner</item>
    <item name="android:padding">5dp</item>
    <item name="android:textColor">@color/colorAccent</item>
    <item name="android:textStyle">bold</item>

</style>

File: Your fragment or Activity

<androidx.appcompat.widget.AppCompatSpinner
    android:id="@+id/spinner_years"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="50dp"
    android:background="@drawable/spinner_bg"
    android:padding="15dp"
    android:theme="@style/MyPopUpTheme" />

Result:

enter image description here

Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97