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.
-
You can customize your own Spinner via Nine patch and specify padding – Badr Mar 20 '15 at 11:33
14 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;
}
- 711
- 8
- 12
-
Worked in my case.. but maybe super view is unnecessary. Just setting 'convertView.setPadding()' should work. – Abu Ruqaiyah Dec 07 '15 at 13:08
-
-
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
}
}
- 9,637
- 6
- 65
- 75
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 itemgetDropdownView(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.
- 12,005
- 6
- 50
- 53
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">
- 147
- 2
-
2Doesn't work. OP is asking about the padding of the text INSIDE the spinner, not the spinner itself. – Le Nguyen Duy Anh May 29 '20 at 08:49
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)
}
}
}
- 3,134
- 19
- 36
- 46
- 71
- 5
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" />
- 145
- 1
- 6
Try this,
Create custom layout(as you need) for spinner row.
inflate the layout when you set the data adapter to the spinner.
- 3,049
- 1
- 32
- 43
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>
- 227
- 2
- 3
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.
- 31,610
- 8
- 53
- 74
- 1,228
- 15
- 26
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.
- 270
- 3
- 7
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" />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 }
- 43
- 1
- 7
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.
- 11
- 3
This Work for me
Spinner spn = (Spinner) findViewById(R.id.spn);
spn.setPadding(0, spn.getPaddingTop(), spn.getPaddingRight(), spn.getPaddingBottom());
- 11,606
- 7
- 43
- 64
- 91
- 1
- 4