Android: Change Spinner Dropdown view

54,743

Solution 1

Kind of reviving an old post here but the accepted answer is far from ideal. The correct way to do this is to set the Spinner to dropdown mode in your layout xml:

<Spinner 
    android:id="@+id/my_spinner"
    ...
    android:spinnerMode="dropdown"/>

The available options are "dialog" and "dropdown".

Solution 2

Your application is running on old theme.

If you are using android 4.2 set android application theme (in the manifest file) to

 android:theme="@android:style/Theme.Holo.Light"

OR

 android:theme="@android:style/Theme.Holo.Light.DarkActionBar"

Solution 3

may be you are running in below than 4.0 , 4.0 will show you dropdown as your image

Solution 4

For the GUI Use HoloEverywhere. https://github.com/Prototik/HoloEverywhere HoloEverywhere is the best way to go if you want Holo theme on older Android then 4.0 .

And for the dropdown use android:spinnerMode="dropdown" in the layout as Stephen Kidson mentioned.

Solution 5

You can use popup like below:

           spinner=(EditText)findViewById(R.id.txt_Spinner);


        spinner.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                p = new Point();
                p.x = location[0]+(v.getHeight());
                p.y = location[1]+v.getHeight();

                if (p != null)
                    showPopup(statusActivity.this, p);

                System.out.println("show popup");
            }
        });




    // The method that displays the popup.
    private void showPopup(final Activity context, Point p) {
        int popupWidth = 300;
        int popupHeight = 500;

        // Inflate the popup_layout.xml
        LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
        LayoutInflater layoutInflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

        // Creating the PopupWindow
        popup = new PopupWindow(context);
        popup.setContentView(layout);
        popup.setWidth(popupWidth);
        popup.setHeight(popupHeight);
        popup.setFocusable(true);

        // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
        int OFFSET_X = 00;
        int OFFSET_Y = 00;

        // Clear the default translucent background
        popup.setBackgroundDrawable(new BitmapDrawable());

        // Displaying the popup at the specified location, + offsets.
        popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
        ((TextView)layout.findViewById(R.id.textView2)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView3)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView4)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView5)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView6)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView7)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView8)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView9)).setClickable(true);

    }

and popup.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/popup_bg"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        style="@style/text_orange_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Status"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Sleeping"
        android:text="Sleeping" />

    <TextView
        android:id="@+id/textView3"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Available"
        android:text="Available" />

    <TextView
        android:id="@+id/textView4"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Busy"
        android:text="Busy" />

    <TextView
        android:id="@+id/textView5"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="At work"
        android:text="At work" />

    <TextView
        android:id="@+id/textView6"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Battery charge low"
        android:text="Battery charge low" />

    <TextView
        android:id="@+id/textView7"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="In meeting"
        android:text="In meeting" />

    <TextView
        android:id="@+id/textView8"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="TMS me later"
        android:text="TMS me later" />

    <TextView
        android:id="@+id/textView9"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="At the toilet"
        android:text="At the toilet" />

    <EditText
        android:id="@+id/textCustomize"
        style="@style/text_blue_contains"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:tag="Customize"
        android:text="Customize" />

</LinearLayout>
Share:
54,743
Sridhar
Author by

Sridhar

PHP developer, Android Developer, iOS Developer profile for Sridhar at Stack Overflow, Q&amp;A for professional and enthusiast programmers http://stackoverflow.com/users/flair/1085737.png?theme=dark

Updated on August 01, 2020

Comments

  • Sridhar
    Sridhar almost 4 years

    Im My application I want the below type of Spinner Dropdown view .enter image description here For this type of spinner view. I wrote this code.

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.spinner, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_obj.setAdapter(adapter);
    

    I got this from http://developer.android.com/guide/topics/ui/controls/spinner.html But what I got is, enter image description here

    Please provide me the best way to do this....

  • Sridhar
    Sridhar over 11 years
    Is there any option to fit AutocompleteTextView to Spinner?
  • Nandagopal T
    Nandagopal T over 11 years
    Please refer the above link and let me know, Are you looking for a widget like that?
  • Saty
    Saty almost 11 years
    Yea, Its nothing to do with theme.... Just use this attribute across any version of android!!
  • Fatima
    Fatima about 10 years
    What should one do if using android 2 ? I have the same problem with android 2.
  • Sanket Kachhela
    Sanket Kachhela about 10 years
    instead of spinner you can also use PopupWindow developer.android.com/reference/android/widget/PopupWindow.h‌​tml
  • mr5
    mr5 about 10 years
    No resource identifier found for attribute 'spinnerMode' in package 'android', now what? It's not available on older version?
  • Hamidreza Sadegh
    Hamidreza Sadegh almost 10 years
    same problem, not change
  • beckah
    beckah almost 9 years
    This made my application crash :/
  • sapht
    sapht over 7 years
    A crash here is probably because of conflict between Actionbar and Toolbar setup (choose the non-actionbar theme if using toolbar).