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):

