1

I would like to have a dropdown spinner with prompt but I have trouble configuring it. Right now I managed to display the prompt by overriding getItem in my adapter but it is ignoring the padding for some reason.

        category = (Spinner) view.findViewById(R.id.category);


        List<String> categoryList = Arrays.asList(getResources().getStringArray(R.array.categories));
        categoryAdapter = new ArrayAdapter<String>(getContext(), R.layout.spinner_item, categoryList){


            @Override
            public int getCount() {
                if (!firsttime) {
                    firsttime = true;
                    return super.getCount(); // you dont display last item. It is used as hint.
                }
                return super.getCount()-1;
            }

        };
        categoryAdapter.setDropDownViewResource(R.layout.spinner_item);

        category.setAdapter(categoryAdapter);

        category.setSelection(categoryAdapter.getCount());

spinner in xml:

<Spinner
                style="@style/Widget.AppCompat.DropDownItem.Spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/category"
                android:spinnerMode="dropdown"
                android:paddingBottom="5sp"
                android:paddingTop="5sp"
                android:paddingStart="15sp"
                android:paddingEnd="15sp"
                android:backgroundTint="@color/text_selected" />

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textColor="@color/text_selected"
    android:padding="5sp"
    />

String array:

 <string-array name="categories">

        <item>For Sale</item>
        <item>Services</item>
        <item>Vehicles</item>
        <item>Property</item>
        <item>Category</item>
    </string-array>

Before I select anything, it looks like this (there is no padding): enter image description here

when I select something: enter image description here

2 Answers 2

3

You need to declare your spinner height

<Spinner
                style="@style/Widget.AppCompat.DropDownItem.Spinner"
                android:layout_width="match_parent"
                android:layout_height="40dp" //like this
                android:id="@+id/category"
                android:spinnerMode="dropdown"
                android:paddingBottom="5sp"
                android:paddingTop="5sp"
                android:paddingStart="15sp"
                android:paddingEnd="15sp"
                android:backgroundTint="@color/text_selected" />
Sign up to request clarification or add additional context in comments.

1 Comment

I might do that at the end.
1

in your spinner item.xml: you are using sp for padding use dp instead

 <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textColor="@color/text_selected"
    android:padding="6dp"   //change here
    />

4 Comments

There is no change :(
did u add the dp values in main xml also
try removing the style in spinner tag...see this Example
I remove the style and didn't see a single change. When I add padding to the relative layout that hosts the spinner, Category is still cut in half.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.