Search code examples
androidandroid-layoutandroid-spinner

Android remove padding from Spinner's text


How can I remove the padding from around the Spinner's text?

enter image description here

My layout:

<?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="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="#ff00ff"
        android:spinnerMode="dropdown" />

    <TextView
        android:id="@+id/textLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Label"
        android:layout_marginStart="8dp"
        android:background="#0000ff"
        android:textColor="#ffffff"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

Activity does not do anything special just put some content into the Spinner. Please let me know if you need it, I'll attach it then.

I'd like to make the Spinner's text to be exactly aligned to Label while the purple box also aligned to blue one. I tried to set padding to 0dp, but it does not change anything. Also tried removing the background, but it also does not change this padding.

And of course if I mess with margins, I can make the texts to be in line, but then the boxes will not stay where they are now.

How shall I do this?

Using a custom overridden View seems to work but why are padding on the top and bottom?

enter image description here

I tried even changing all padding to 0, but still.

Here is my actual adapter:

public class MyAdapter extends ArrayAdapter {
    public MyAdapter(@NonNull Context context, int resource, String[] params) {
        super(context, resource, params);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        view.setPadding(0, 0, 0, 0);
        return view;
    }
}

Solution

  • You can try adding this to the adapter.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            view.setPadding(0, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
            return view;
    }