Search code examples
androidkotlindropdownspinner

Dropdown in custom Alertdialog doesn't show any items (Kotlin)


I wanted to create a custom Alertdialog Layout with a dropdown list and a few other things. I'm using Kotlin and I'm pretty new to it Currently I'm stuck at the dropdown list as it doesn't show anything

Here is the Layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <Button
        android:id="@+id/backButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:text="Zurück"
        app:icon="@drawable/ic_baseline_arrow_back_24"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/pizzaSelection"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

And here is how I call up the dialog in my activity:

   private fun showPizzaDialog(){
        val pizzaDialogBuilder = AlertDialog.Builder(this)
        pizzaDialogBuilder.setView(R.layout.pizza_alertdialog)
        
        val pizzaDropdown = findViewById<Spinner>(R.id.pizzaSelection)
        val pizzaTypes = resources.getStringArray(R.array.pizzaTypes)

        if (pizzaDropdown != null) {
            val adapter = ArrayAdapter(this,
                android.R.layout.simple_spinner_item, pizzaTypes)
            pizzaDropdown.adapter = adapter
        }
        
        pizzaDialogBuilder.show()

    }

The Items are currently hardcoded in the strings.xml resource file as a string-array.

The AlertDialog shows up and i can see and click on the arrow for the dropdown menu, but when I click on it nothing happens.


Solution

  • You're calling findViewById on your current Activity, which doesn't contain R.id.pizza_selection. Therefore I suspect you'll see

    val pizzaDropdown = findViewById<Spinner>(R.id.pizzaSelection)

    return null.

    Try something like this:

    // inflate your layout    
    val dialogView = LayoutInflater.from(this).inflate(R.layout.pizza_alert_dialog, null, false)
         
    // and set it as dialog view          
    pizzaDialogBuilder.setView(dialogView)
        
    // then call findViewById on this ViewGroup to get the Spinner            
    val pizzaDropdown = dialogView.findViewById<Spinner>(R.id.pizzaSelection)
    

    my Kotlin syntax might not be correct, sorry. Important is that we're calling findViewById on dialogView, instead of the implicit this.findViewById()