How to change spinner layout in Android

When O clicks on this counter, it gives a large drop-down menu:

enter image description here

I need a very small representation, as in the second image. Just like drop-down lists in ASP.NET. Similarly with reduced width.

enter image description here

I used the following code. Any help in changing the look of the second image would be appreciated.

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
       this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
+5
source share
3 answers

This is a good article: Customizing the action bar

And also you can try the following:

Create your own custom template for the spinner's background and apply it to it. For spinnerbackground.xml images, you can reference images from the SDK. recreate images according to your design requirements

"Android-SDK\\-9\Data\\-\*. PNG"

spinnerbackground.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/wlbtn_dropdown_normal" />
    <item
        android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/wlbtn_dropdown_disabled" />
    <item
        android:state_pressed="true"
        android:drawable="@drawable/wlbtn_dropdown_pressed" />
    <item
        android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/wlbtn_dropdown_selected" />
    <item
        android:state_enabled="true"
        android:drawable="@drawable/wlbtn_dropdown_normal" />
    <item
        android:state_focused="true"
        android:drawable="@drawable/wlbtn_dropdown_disabled_focused" />
    <item
        android:drawable="@drawable/wlbtn_dropdown_disabled" />
</selector>

spinner :

<Spinner android:background="@drawable/spinnerbackground"
         android:id="@+id/spinnerIDr"
         android:layout_height="wrap_content" 
         android:layout_width="fill_parent">
    </Spinner>

:

<Spinner android:background="@drawable/spinnerbackground"
         android:id="@+id/spinnerIDr"
         android:popupBackground="@drawable/popup_background"
         android:layout_height="wrap_content" 
         android:layout_width="fill_parent">
    </Spinner>

popup_background: enter image description here

(name: custom_spiner.xml)

<?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="fill_parent"
    android:padding="4dp"
    android:textSize="14sp"
    android:typeface="serif"
    android:singleLine="true"
    android:layout_marginLeft="2dip"
    android:layout_marginRight="5dip"
    android:ellipsize="marquee"
    android:textColor="#000000">
</TextView>

adapter.setDropDownViewResource(R.layout.custom_spiner);

.

2:

, Java, PopupWindow

: custom-spinner

+9

. .

(1) . , "@android: drawable/btn_dropdown". , . , , .

(2) . Dialog .

this.setContentView(R.layout.dropdownlist);

,

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = dlgWidth;
lp.height = dlgHeight;
lp.dimAmount = 0;
getWindow().setAttributes(lp);
getWindow().setBackgroundDrawableResource(mBackgroundResId);

(3) "OnItemClickListener", , .

, .

+1

Spinner

Android: dropDownWidth = "150dp"

+1
source

All Articles