Andrew El-Masry
Andrew El-Masry

Reputation: 129

Avoid showing selected text in Spinner

The default behavior of the android spinner is to initially display the first item in the top of the dropdown portion of the spinner. As the user selects other items/text in the dropdown, it will update the text at the top with the selected item.

I don't want any text to show up at all in the top part of the spinner. Without having an empty sting as the first item, because that leaves a gap. Does anyone know how to do that? This is what I have so far


styles.xml

<resources>
    <style parent="@android:style/Widget.TextView.PopupMenu" name="SpinnerAsEditText">
        <item name="android:background">@drawable/filter_arrow</item>
    </style>
</resources>


this is my layout: main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/colorAccent"
    android:layout_height="match_parent">

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/list"
        style="@style/SpinnerAsEditText"/>
 </RelativeLayout>


This is a visual representation of my problem - if you click on this image/gif, it should animate my problem.

enter image description here

Upvotes: 1

Views: 1914

Answers (3)

Rahul Devanavar
Rahul Devanavar

Reputation: 4165

You can use popUpWindow to show dropDownList.

<ImageView
    android:id="+@id/spinnerImage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="showSpinner" />

in java

findViewById(R.id.spinnerImage).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             mSortPopupWindow = popupWindow(spinnerItemList);
                    mSortPopupWindow.showAsDropDown(v, -5, 0); // This is impotent to where to show drop down   
            }
        });

 public PopupWindow popupWindow(String[] stringArray) {
        PopupWindow popupWindow = new PopupWindow(mContext);
        ListView listView = new ListView(mContext);

        listView.setAdapter(dropDownAdapter(stringArray));

        listView.setOnItemClickListener( AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(mContext, "Selected" + stringArray[position], Toast.LENGTH_SHORT).show();
            popupWindow.dismiss();
        }

    });

        popupWindow.setFocusable(true);
        popupWindow.setWidth(250);//Or you can set wrap_content
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

        popupWindow.setContentView(listView);

        return popupWindow;
    }

Upvotes: 1

Cory Roy
Cory Roy

Reputation: 5609

You could use an image which when clicked on shows the spinner. Initially the spinner would be hidden.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:layout_height="match_parent">
<ImageView
    android:id="+@id/spinnerImage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="showSpinner" />
<Spinner
    android:id="+@id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/list"
    style="@style/SpinnerAsEditText"
    android:visibility="hidden"/>

and in Java:

void showSpinner() {
   findViewById(R.id.spinnerImage).setVisibility(View.GONE);
   findViewById(R.id.spinner).setVisibility(View.VISIBLE);
}

Upvotes: 0

santosh kumar
santosh kumar

Reputation: 2962

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/colorAccent"
    android:layout_height="match_parent">

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/list"
        style="@style/SpinnerAsEditText"/>
 </RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>  
    <resources>  
     <array name="list">  
    <item></item>  
    <item>second</item>  
    <item>third</item>  
    <item>fourth</item>  
    <item>fifth</item>  
     </array>
    </resources>

Upvotes: 0

Related Questions