45

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

bytecode77
  • 14,163
  • 30
  • 110
  • 141
BomberBus
  • 1,195
  • 3
  • 15
  • 29

14 Answers14

29

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;
}
Chan Chun Him
  • 791
  • 8
  • 12
8

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
    }
}
5

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">
HUAN XIE
  • 175
  • 1
  • 3
  • 3
    Doesn't work. OP is asking about the padding of the text INSIDE the spinner, not the spinner itself. – Sorry May 29 '20 at 08:49
4

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.

hidro
  • 12,333
  • 6
  • 53
  • 53
4

This Kotlin code is a little bit more succinct than the getView override in another answer. It makes it a bit more clear in fewer lines that it is mostly just doing what super does, except that it also overrides setPaddingfor the left value.

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

Here is my full fragment code that uses a data binding object for the complete spinner array adapter override, which also does a selected item and an alternating background color in getDropDownView:

binding.editSpinner.apply {
    this.context ?: return@apply
    this.adapter = object : ArrayAdapter<Any>(
        requireContext(),
        android.R.layout.simple_spinner_dropdown_item,
        SpinnerTypeValues.values().map { it.text }
    ) {
        override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
            return super.getDropDownView(position, convertView, parent).also {
                when {
                    position == this@apply.selectedItemPosition -> it.setBackgroundColor( Color.rgb(245, 212, 119) )
                    position % 2 == 0 -> it.setBackgroundColor(Color.rgb(240, 240, 240))
                    else -> it.setBackgroundColor(Color.rgb(214, 235, 189))
                }
            }
        }
        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            return super.getView(position, convertView, parent).apply {
                setPadding(0, paddingTop, paddingRight, paddingBottom)
            }
        }
    }
    this.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(parent: AdapterView<*>?) {}
        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            viewModel.saveSpinnerSelection(position)
        }
    }
}
Alessio
  • 3,404
  • 19
  • 35
  • 48
Daniel Gibby
  • 101
  • 6
2

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" />
Punit Shah
  • 145
  • 1
  • 6
2

Replace android.R.layout.simple_spinner_item layout from code by new spinner_item layout

Code for spinner_item:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/primaryText"
    android:textColor="@color/black"
    android:textStyle="bold"/>

This will remove an extra margin and you can style it according to your requirement.Thanks.

Alex
  • 41
  • 4
1

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.

Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45
1

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>
Rudy Rudolf
  • 227
  • 2
  • 3
1

in your onViewCreated()

spinner.post { spinner.setPadding(0,0,0,0) } 
Jan Rabe
  • 398
  • 2
  • 12
1

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.

adiga
  • 34,372
  • 9
  • 61
  • 83
azwar_akbar
  • 1,451
  • 18
  • 27
0

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.

Pete
  • 280
  • 3
  • 7
0
  1. Create a text-view layout view_spinner_text_view.xml:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />
    
  2. Create the adapter by providing the above layout:

        ArrayAdapter.createFromResource(
            context,
            R.array.my_values,
            R.layout.view_spinner_text_view // <-- this is for the spinner itself
        ).also { adapter ->
            adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item // <-- this is for the dropped-down items (you can customize this one as well)
            )
            spinner.adapter = adapter
        }
    
-2

This Work for me

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

spn.setPadding(0, spn.getPaddingTop(), spn.getPaddingRight(), spn.getPaddingBottom());
Sagar V
  • 12,158
  • 7
  • 41
  • 68
Avinash Kahar
  • 129
  • 1
  • 5