How to programmatically set drawableStartCompat on Android Spinner Edittext

Viewed 932

I'm initializing a dropdown menu with an icon (country flag) and the phone nr. prefix (String). When I set the drawable in XML, it works:

app:drawableStartCompat="@drawable/flag_afn"

But when I programmatically find the view, using

setCompoundDrawablesWithIntrinsicBounds 

it doesn't work.

MainActivity.kt

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val viewGroup =
            (findViewById<View>(android.R.id.content) as ViewGroup).getChildAt(0) as ViewGroup

        initializeSpinner(viewGroup)
    }

    private fun initializeSpinner(parent: ViewGroup) {
        ArrayAdapter.createFromResource(
            this,
            R.array.phone_country_codes,
            R.layout.spinner_dropdown_item
        ).also { adapter ->
            // Specify the layout to use when the list of choices appears
            adapter.setDropDownViewResource(R.layout.spinner_dropdown_item)
            // Apply the adapter to the spinner
            phonePrefix.adapter = adapter

            for (cnt in 0 until adapter.count) {
                val drawable = ContextCompat.getDrawable(this, R.drawable.flag_bmd)

                val view = adapter.getView(cnt, null, parent) as AppCompatTextView
                view.text = "TEST"
//                view.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null,  null)
//                view.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
                view.setCompoundDrawablesWithIntrinsicBounds(R.drawable.flag_bmd, 0, 0,0)
            }
        }
    }

activity_main.xml

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

    <Spinner
        android:id="@+id/phonePrefix"
        android:layout_width="180dp"
        android:layout_height="40dp"
        android:layout_margin="8dp" />

</LinearLayout>

spinner_dropdown_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView 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:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:maxLength="4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="inherit"
    tools:text="+31"
    app:drawableStartCompat="@drawable/flag_afn" />

phone_country_codes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="phone_country_codes">
        <item>+31</item>
        <item>+49</item>
        <item>+41</item>
    </string-array>
</resources>

This screenshot shows the "+31" instead of "TEST" and the flag that was added in the xml.

enter image description here

0 Answers
Related