0

for now radio button image on shown on right side of my spinnerand when I click on it, it open option list in popup box

what I want is to show arrow on right side of spinner and options list should be drop down instead of popup box with white background.

see image

enter image description here

How Can I do this, Do I need to create a custom spinner?

Here is the code

XML

<Spinner
android:id="@+id/type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/text_color"/>

Java

type_Spinner = (Spinner) findViewById(R.id.type_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
type_Spinner.setAdapter(adapter);
Bilal Haider
  • 173
  • 1
  • 3
  • 14

2 Answers2

1

1st approach

Change Spinner in xml like this

<Spinner
    android:id="@+id/type_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/btn_dropdown" />

Change theme of that Activity to

android:Theme.Holo

In java class

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
type_Spinner.setAdapter(adapter);

2nd approach

Instead of Spinner use a Button set background using android:background="@android:drawable/btn_dropdown" set gravity (not layout_gravity) to left|center_vertical open a PopupWindow on clicking of that Button set that Button as anchor of that PopUpWindow. In that PopUpWindow place a ListView and in OnItemClick change text with selected value in that Button using setText(java.lang.CharSequence)

Full code snippet for 2nd approach

If you use approach 1 then it will work on Post Gingerbread version. Approach 2 will work on any version of android(not tested in pre froyo).

Community
  • 1
  • 1
Kaushik
  • 6,150
  • 5
  • 39
  • 54
1

In layout use the android:spinnerMode

android:spinnerMode="dropdown"

In activity use like this:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
type_Spinner.setAdapter(adapter);
Remees M Syde
  • 2,564
  • 1
  • 19
  • 42