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 spac
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;
}
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.
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" />
This Work for me
Spinner spn = (Spinner) findViewById(R.id.spn);
spn.setPadding(0, spn.getPaddingTop(), spn.getPaddingRight(), spn.getPaddingBottom());
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
}
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.