Android: Custom spinner size problems

18,564

Solution 1

while setting adapter say adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); this should work...

Solution 2

Use the below code as per your need...

ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.Questions, R.layout.custom_spinner_list);
            adapter.setDropDownViewResource(R.layout.customer_spinner);
            spin.setAdapter(adapter);

custom_spinner_list.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="@+id/TextView01"
    android:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="12dp"
    android:textColor="#000000">
</TextView>

customer_spinner.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="@+id/TextView01"
    android:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="12dp"
    android:textColor="#000000"
    android:height="42dp"
    android:gravity="center_vertical"
    android:padding = "2dp">
</TextView>

Solution 3

Maybe this is quite late but I'll share this anyway because I came across a similar problem.

You need to use the Views "simple_spinner_item" for the Spinner itself and "simple_spinner_dropdown_item" for the dropdown.

Here's a code snippet:

Spinner spnCat = (Spinner) findViewById(R.id.idea_category);

Cursor c = myDBHelper.getCategories();
startManagingCursor(c);

if (c != null) {
    // use the simple_spinner_item view and text1
    SimpleCursorAdapter adapterCat = new SimpleCursorAdapter(this,
        android.R.layout.simple_spinner_item,
        c, 
        new String[] {c.getColumnName(1)}, 
        new int[] {android.R.id.text1});

    spnCat.setAdapter(adapterCat);

    // use the simple_spinner_dropdown_item
    adapterCat.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
}

Hope that helps.

Share:
18,564
Nick
Author by

Nick

Updated on June 04, 2022

Comments

  • Nick
    Nick almost 2 years

    I've made a custom spinner but the sizing isn't exactly what I want it to be. The spinner become far to large to get the spacing in the list that I need. I want to be able to size the rows of the spinner independently of the size of the spinner button. I want the spinner to be thin and then I want the section rows to be spaced generously. (See image at the bottom):

    Currently the xml for the rows of the spinner is this:

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:padding="5dip">
            <ImageView android:layout_width="32sp" android:src="@drawable/icon"
                android:id="@+id/spinnerimage" android:layout_height="32sp" />
            <TextView android:textSize="22sp" android:textStyle="bold" android:textColor="#000"
                android:layout_width="fill_parent" android:id="@+id/category"
                android:layout_height="fill_parent" android:paddingLeft="5sp" />
        </TableRow>
    </TableLayout>
    

    I wanted to use a relative layout but the table layout gave me slightly better spacing. If I try to make the height it will cut off the text and icons in the rows. The spinner in my main.xml is:

            <Spinner android:id="@+id/catspinner"
            android:layout_marginLeft="25dip" android:layout_marginRight="25dip"
            android:layout_width="fill_parent" android:layout_centerHorizontal="true"
            android:layout_height="wrap_content" android:prompt="@string/prompt"
            android:background="@drawable/yellow_btn"
            android:layout_centerVertical="true" android:drawSelectorOnTop="true" />
    

    enter image description here

    I would like to have the spinner the sizing to be the same as the standard android spinner (on right.) Mine is currently reversed the spinner is to big and the rows spacing is too small.

    Any ideas??