dev_android
dev_android

Reputation: 8818

Android spinner layout

I have set a dynamic value in a spinner . I am using following code for the same.

spinner_generalbooks.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, new String[]{"Author","ISBN","Keyword","Title"}));

It is working fine, but I have a problem with the view of the spinner . If we select simple_spinner_item, it is ok in normal state, but when we want to change it, the rows are very narrow and not CheckedTextView whereas in normal spinner options comes with CheckedTextView. If we select simple_spinner_dropdown_item, the options come with CheckedTextView, but in normal state, it looks different as in the pic

(First one is default spinner and second one is using simple_spinner_dropdown_item).

First one is default spinner and second one is using <code>simple_spinner_dropdown_item</code>

I want to show the spinner just like as default spinner. How to make it?

Upvotes: 4

Views: 4995

Answers (2)

MindWire
MindWire

Reputation: 4089

Android will take the layout specified in the adapter and use it for the control and the items unless you specify the view-resource separately. The way through this is to set the layout to simple_item in the ArrayAdapter constructor and then set the layout dropdown_item separately in a call to setDropDownViewResource().

ArrayAdapter newAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, new String[]{"Author","ISBN","Keyword","Title"});

newAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner_generalbooks.setAdapter(newAdapter);

Upvotes: 5

If you whant to customize the visual of your Spinner, it'll be more simple to create your own component. A spinner is just a Layout that contains text, image and that display a list in a popup. Create a custom layout for your item and use a new BaseAdapter object to bind your datas.

http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/

hope i could help

Upvotes: 1

Related Questions