A13X
A13X

Reputation: 436

Spinner Dropdown won't center or match Parent Width

The spinner dropdown has been finicky. The only thing I could think of is a parent view is causing it to behave such that either the text or the whole dropdown list itself gets off-center from the spinner item like so:

enter image description here

Dropdown Code:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="center"
android:textColor="#000000"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"/>

Spinner Code:

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinner_contour"
    android:layout_below="@id/greeting_main_menu"
    android:background="#ff515e6f"
    android:layout_alignParentRight="true"
    android:layout_marginTop="10.0sp"
    android:layout_marginBottom="10.0sp"
    android:popupBackground="#ffffff"
    android:paddingStart="20dp"
    android:paddingEnd="20dp"
    android:paddingBottom="10dp" />

I've definitely tried the obvious margins and padding additions to the dropdown items, I'm at a loss. I want the white dropdown to line up or at least have the same width as the light blue spinner.

EDIT: Here is the adapter code I use for the list at least, it's pretty basic:

public void createSpinners()
{
    spinnerContour = (Spinner) activity.findViewById(R.id.spinner_contour);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(activity.getApplicationContext(),
            R.array.contour_values, R.layout.custom_spinner_item);
    adapter1.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
    spinnerContour.setAdapter(adapter1);
    spinnerContour.setOnItemSelectedListener(this);

    spinnerBPM = (Spinner) activity.findViewById(R.id.spinner_bpm);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(activity.getApplicationContext(),
            R.array.bpm_values, R.layout.custom_spinner_item);
    adapter2.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
    spinnerBPM.setAdapter(adapter2);
    spinnerBPM.setOnItemSelectedListener(this);
}

Upvotes: 0

Views: 2747

Answers (1)

A13X
A13X

Reputation: 436

So it turns out the padding of the spinner determines the offset of the dropdown list. Go figure!

This seemed to be the answer: https://stackoverflow.com/a/29560301/1580355

While I am now trying to align the text center, at least the dropdown and spinner widths are now equal. Thank you for taking a look.

Upvotes: 1

Related Questions