Foenix
Foenix

Reputation: 991

Custom row in Spinner

I just need that items in a Spinner rows wraps when their length is large. So I did this:

item.xml

<TextView
    android:id="@+id/tvAddr"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:text="Address"
    android:textSize="14sp" />

layout with Spinner:

...

   <Spinner
        android:id="@+id/spAddr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

...

and a code to implement data

Cursor c = tbl.getAddr(id);

    getActivity().startManagingCursor(c);
    String[] from = new String[] { "address" };
    int[] to = new int[] { android.R.id.text1 };
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_spinner_item, c,
            from, to);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner s = (Spinner) v.findViewById(R.id.spAddr);
    s.setAdapter(adapter);

But I do not know how to use my row-resource item.xml in here. How can I implement my custom row?

UPD: after I change item.xml with MSquare code.

Closed Spinner

Closed Spinner

Opened Spinner

Opened Spinner

And when I choose item I've got picture 1 again.

Upvotes: 0

Views: 1413

Answers (1)

MSquare
MSquare

Reputation: 6469

Try this:

Your item.xml should be like this:

<?xml version="1.0" encoding="utf-8"?>

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:text="Address"
    android:textSize="14sp" />

and in the code you can use now:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), 
        R.layout.item, c, from, to);

Try it out.

Upvotes: 1

Related Questions