Search code examples
androidxmlspinner

How to make the list of spinner items rounded? Not the spinner itself, but dropdown list


I want to display the items of spinner in rounded rectangle list, not the default rectangle. I don't mean the spinner itself, but the list which falls after arrow is clicked. Is there a way to do this? The attempt to make custom_rounded_layout and attaching to it adapter changed the layout of each item, not the list itself. However, I want the list itself to be rounded, not the items. Right now the list is the default one. I want to make the corners of the list a bit rounded. Thanks in advance


Solution

  • Create a layout file with a rounded background textview like this:

    spinner_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"
        android:layout_width="match_parent"
        android:layout_height="@dimen/size_40"
        android:background="@drawable/bg_round"
        android:gravity="center_vertical"
        android:id="@+id/ssdsd"
        android:layout_margin="16dp"
        android:padding="10dp"
        android:text="@string/app_name" />
    

    Now pass this in the spinner adapter

    Spinner spinner = findViewById(R.id.yourSpinner);
    spinner.setAdapter(new ArrayAdapter<String(this,R.layout.spinner_item,R.id.ssdsd,list));
    

    To change the popup background, use this on your Spinner xml code:

    
            android:popupBackground="@drawable/bg_round"
    

    NB: You might need to adjust padding and margin to make it look good, this is just an idea of how you can do it.