Home » Android » Remove Spacing or Padding in android Spinner

Remove Spacing or Padding in android Spinner

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m using android spinner and edittext from support lib v21. I would like to align text to left the same like edittext as shown in figure. But, deafult spinner include spacing or padding. So, I would like to remove it to align text to left. Please help how to remove it.

enter image description here

How to&Answers:

In your SpinnerAdapter:

@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;
}

Answer:

This array adapter removes left padding in Android Spinner.

Kotlin version:

open class NoPaddingArrayAdapter<T>(context: Context, layoutId: Int, items: List<T>?) : ArrayAdapter<T>(context, layoutId, items) {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        val view = super.getView(position, convertView, parent)
        view.setPadding(0,view.paddingTop,view.paddingRight,view.paddingBottom)
        return view
    }
}

Answer:

You can also remove the padding of spinner in xml

Only need to set padding to 0dp.

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:padding="0dp"     
        android:id="@+id/my_spinner">

Answer:

Try below code to do so. Change style attribute in your spinner list item layout to textViewStyle as below

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/textViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="false"
    android:textAlignment="inherit"
    android:textAppearance="?android:attr/textAppearanceMedium" />

Answer:

The layout for TextView that is displayed in your Spinner comes from your SpinnerAdapter, which provides 2 methods:

  • getView(position, convertView, parent): returns View for collapsed state, you can override this to return different layouts for different selected item
  • getDropdownView(position, convertView, parent): return View for each dropdown item in expanded state (after you click Spinner to open popup/dialog for selection)

In your case you should override your SpinnerAdapter.getView(position, convertView, parent) and inflate the layout with padding/spacing of your choice.

For example:

spinner.setAdapter(new MyAdapter());

MyAdapter.java

class MyAdapter implements SpinnerAdapter {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.spinner_item, parent, false);
        }
        // bind your data here
        return convertView;
    }
    // other implementation
}

res/layout/spinner_item.xml

<!-- set padding margin whatever here -->
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

You can also conveniently use one of the framework provided SpinnerAdapter indirect subclasses (ArrayAdapter<T>, BaseAdapter, CursorAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, ThemedSpinnerAdapter), just make sure to supply the correct layout that would be used by getView() method.

Answer:

Try this,

  1. Create custom layout(as you need) for spinner row.

  2. inflate the layout when you set the data adapter to the spinner.

Answer:

If yo don’t want to mess in Java Code and have it in styles

Search for this file (Cmd+Shift+O)
abc_spinner_textfield_background_material.xml

and copy to your project. Remove all inset values from root element and refer the file as background of your spinner.

In my case it looks like this (drawable/spinner_background.xml)

<inset xmlns:android="http://schemas.android.com/apk/res/android">
    <selector>
        <item
            android:state_checked="false"
            android:state_pressed="false">
            <layer-list>
                <item android:drawable="@drawable/abc_textfield_default_mtrl_alpha" />
                <item android:drawable="@drawable/abc_spinner_mtrl_am_alpha" />
            </layer-list>
        </item>
        <item>
            <layer-list>
                <item android:drawable="@drawable/abc_textfield_activated_mtrl_alpha" />
                <item android:drawable="@drawable/abc_spinner_mtrl_am_alpha" />
            </layer-list>
        </item>
    </selector>
</inset>

and in my styles.xml I use it this way

<style name="DxSpinner" parent="@style/Widget.AppCompat.Spinner.Underlined">
    <item name="android:background">@drawable/spinner_background</item>
</style>

Answer:

Maybe this is the simplest way and also it just work. To decrease left padding of Spinner, you can use set padding left to minus so it will move to the left. For example android:layout_marginLeft="-8dp" This is not a best practice, only a simple hack. Here is a code sample:

<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="-8dp"
android:entries="@array/arr_tipe_kendaraan"
android:gravity="left"
android:paddingTop="14dp"
android:paddingBottom="8dp"
android:prompt="@string/action_sign_in"
android:spinnerMode="dialog" />

Hope this helps.

Answer:

I just hit this issue as well. I was able to solve it with adjusting margin actually instead of padding, so setting android:marginStart=”0dp” to the spinner item.

Answer:

in your onViewCreated()

spinner.post { spinner.setPadding(0,0,0,0) } 

Answer:

This Work for me

Spinner spn = (Spinner) findViewById(R.id.spn);

spn.setPadding(0, spn.getPaddingTop(), spn.getPaddingRight(), spn.getPaddingBottom());