Android Spinner: central text vertically inside the spinner

lol

I need these spinners to center the text vertically. I tried such things in the Spinner xml definition:

<Spinner android:id="@+id/dir_spn" android:layout_gravity="center_vertical" android:gravity="center_vertical" android:layout_weight="1"/> 

I tried to create a custom folding layout:

 <?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="@style/SpinnerDropDownItem" android:singleLine="true" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:ellipsize="marquee" /> 

styles.xml:

 <resources> <style name="SpinnerDropDownItem"> <item name="android:gravity">center_vertical</item> <item name="android:layout_gravity">center_vertical</item> <item name="android:textColor">?android:attr/textColorPrimary</item> <item name="android:textColorHighlight">#FFFF9200</item> <item name="android:textColorHint">?android:attr/textColorHint</item> <item name="android:textColorLink">#5C5CFF</item> <item name="android:textSize">16sp</item> <item name="android:textStyle">normal</item> </style> </resources> 

adapter dropdown set

:

 adapter.setDropDownViewResource(R.layout.spinner_dropdown_item); 

How to get Spinner text vertically?

EDIT: Maybe it's worth mentioning that the Activity is defined in the manifest as follows:

 <resources> <style name="SpeakNSpell" parent="@android:style/Theme"> <item name="android:minHeight">68sp</item> </style> </resources> 

making all kinds of at least 68sp. Does this also affect the text inside Spinner?

+6
source share
3 answers

Rewrite

It seems you want to center the text vertically in the element resource, as shown below, but your code examples are aimed at the drop-down menu ...

Compare Layouts

The top Spinner is the default layout android simple_spinner_item by default, and the bottom is the same, but android:gravity="center_vertical" added android:gravity="center_vertical" .

 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/spinnerItemStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:gravity="center_vertical" android:singleLine="true" /> 

You can see that I used your theme with great minHeight . Here is a basic example of using this layout in an adapter:

 Spinner spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, list); // set whatever dropdown resource you want spinner.setAdapter(adapter); 

I went with the assumption that you want CASH, Select Product, etc. focused on the shot, let me know if you are trying to do something else!

+8
source

If the user still has problems after installing gravity , try installing the Spinner add-on:

  android:paddingTop="0dp" android:paddingBottom="0dp" 
+3
source

In your SpinnerAdapter try something like this:

 public View getDropDownView(int position, View convertView, ViewGroup parent) { View v = super.getDropDownView(position, convertView,parent); ((TextView) v).setGravity(Gravity.CENTER); return v; } 
0
source

Source: https://habr.com/ru/post/923496/


All Articles