Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android Spinner: center text vertically within spinner

lol

i need these spinners to vertically center the text. i've tried stuff like this 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 building a custom drop down 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 do i get the Spinner text to center vertically?

EDIT: it may be worth mentioning that the Activity is themed in the manifest thusly:

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

which makes all Views at least 68sp high. is this affecting the text inside the Spinner as well?

like image 330
moonlightcheese Avatar asked Aug 22 '12 16:08

moonlightcheese


2 Answers

Rewrite

You seem to want to center the text vertically in the item resource, shown below, but your code examples targets the dropdown menu...

Compare Layouts

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

<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 the large minHeight. Here is a basic example to use 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 from your picture centered, let me know if you where trying to do something else!

like image 108
Sam Avatar answered Nov 15 '22 06:11

Sam


If somebody is still having problems after setting gravity, try to set padding of the Spinner:

                android:paddingTop="0dp"
                android:paddingBottom="0dp" 
like image 27
peter.bartos Avatar answered Nov 15 '22 06:11

peter.bartos