I have a SearchableSpinner and I want to display the text in the center of it
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <com.toptoche.searchablespinnerlibrary.SearchableSpinner android:id="@+id/spinner_client" style="@style/spinner_style_v2" android:layout_width="300dp" android:layout_height="wrap_content" android:padding="5dp" android:drawSelectorOnTop="true"/> </LinearLayout>
and here the custom style. I tried adding <item name="android:textAlignment">center</item>
but it’s not working also I’ve tried <item name="android:paddingTop">10dp</item>
to see if the text will move to the bottom a little bit but it’s not moving.
<style name="spinner_style_v2"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:background">@drawable/gradient_spinner</item> <item name="android:popupBackground">#DFFFFFFF</item> </style>
Spinner adapter
try { if (clientList != null && clientList.size() > 0) { ArrayAdapter<Client> clientArrayAdapter = new ArrayAdapter<Client>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item, clientList); clientArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); clientSpinner.setAdapter(clientArrayAdapter); clientSpinner.setTitle("Choose Client"); } } catch (Exception ex) { Log.d(TAG, ex.toString()); }
Answer
To center item in your Spinner create a Custom layout and center TextView with android:gravity="center"
like the XML below:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> </TextView>
and in your java code use this layout as @LayoutRes in your Adapter :
ArrayAdapter<Client> clientArrayAdapter = new ArrayAdapter<Client>(getApplicationContext(),R.layout.simple_text, clientList);