Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect if the drop down menu is shown above or below the spinner in android?

I m working on design were spinner looks custom what i want is when spinner popup drop down below the spinner i want to used popupbackground image different and when it popup above the spinner i want to use popup background different

here is my Xml code:

 <Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spinnerMode="dropdown"
    android:popupBackground="@drawable/spinnerbottombg"
    android:overlapAnchor="false"
    android:drawSelectorOnTop="false"

    />

now question is how do i know spinner open the dropdown list above or below it

like image 832
Nawaf Avatar asked Apr 07 '16 01:04

Nawaf


1 Answers

I dig deep in the PopupWindow and found that your requirement could be achieved by

StateListDrawable

Following codes will explain everything-

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.mmbarno.dummyalertdialog.MainActivity">

   <Spinner
       android:id="@+id/spinner"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:spinnerMode="dropdown"
       android:overlapAnchor="false"
       android:drawSelectorOnTop="false" />
</RelativeLayout>

popup_bg_above.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
   <solid android:color="#FF4081" />
</shape>

popup_bg_below.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
   <solid android:color="#3F51B5" />
</shape>

Lastly in MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setPopupBackgroundDrawable(getPopupBg());
    populateSpinner();
}

private void populateSpinner() {
    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);
}

private StateListDrawable getPopupBg() {
    StateListDrawable states = new StateListDrawable();
    int identifier = Resources.getSystem().getIdentifier("state_above_anchor", "attr", "android");
    states.addState(new int[] {identifier}, ContextCompat.getDrawable(this, R.drawable.popup_bg_below));
    states.addState(new int[]{}, ContextCompat.getDrawable(this, R.drawable.popup_bg_above));
    return states;
}

Hope your requirement will be fulfilled. I have tested it in different scenarios along with ScrollView. And it perfectly works.

Popup Below

Popup Above

like image 190
Barno Avatar answered Oct 25 '22 21:10

Barno