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

Nawaf

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

Barno

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Swing - Android like Drop-Down-Menu

分類Dev

How to show Icon in drop down menu of PopupMenu

分類Dev

How to drop down menu wordpress only css

分類Dev

How To Make A Decent Drop Down Menu?

分類Dev

How to detect a call Drop in android

分類Dev

How to add options tabbed menu to mega drop-down menu?

分類Dev

how to display all menu and submenu item in a different drop down list?

分類Dev

Drop-down Menu on Hover

分類Dev

Drop down menu doesnt work

分類Dev

CSS drop down menu not appearing

分類Dev

Drop down menu moving down page content

分類Dev

Using webdriver with c# how can I select a drop down item by text if the drop down menu is not a select element?

分類Dev

Fix width of drop down menu in select option

分類Dev

Drop Down - Submenu menu Disappear while hover

分類Dev

drop down menu not working and in wrong position

分類Dev

filtering select drop down menu to filter in React?

分類Dev

Horizontal drop down menu using css

分類Dev

CSS drop down menu weird behavior

分類Dev

Drop down menu for selecting country then states then district

分類Dev

PHP drop down menu list categories

分類Dev

Creating a conditional drop-down menu in Excel

分類Dev

Drop down menu on click on variable id

分類Dev

Rails: creating a drop down menu for a form

分類Dev

Remove underline on Spinner Drop down and Change the color of the triangle

分類Dev

How do I stop links from wrapping onto multiple lines in a CSS drop down menu?

分類Dev

How can I sort a particular column when triggered by a drop down menu on the client side?

分類Dev

How do I switch the direction of the menu drop down when it flows over the width?

分類Dev

how to customize the spinner in android?

分類Dev

Drop down menu for posts as like twitter and facebook implemented

Related 関連記事

  1. 1

    Swing - Android like Drop-Down-Menu

  2. 2

    How to show Icon in drop down menu of PopupMenu

  3. 3

    How to drop down menu wordpress only css

  4. 4

    How To Make A Decent Drop Down Menu?

  5. 5

    How to detect a call Drop in android

  6. 6

    How to add options tabbed menu to mega drop-down menu?

  7. 7

    how to display all menu and submenu item in a different drop down list?

  8. 8

    Drop-down Menu on Hover

  9. 9

    Drop down menu doesnt work

  10. 10

    CSS drop down menu not appearing

  11. 11

    Drop down menu moving down page content

  12. 12

    Using webdriver with c# how can I select a drop down item by text if the drop down menu is not a select element?

  13. 13

    Fix width of drop down menu in select option

  14. 14

    Drop Down - Submenu menu Disappear while hover

  15. 15

    drop down menu not working and in wrong position

  16. 16

    filtering select drop down menu to filter in React?

  17. 17

    Horizontal drop down menu using css

  18. 18

    CSS drop down menu weird behavior

  19. 19

    Drop down menu for selecting country then states then district

  20. 20

    PHP drop down menu list categories

  21. 21

    Creating a conditional drop-down menu in Excel

  22. 22

    Drop down menu on click on variable id

  23. 23

    Rails: creating a drop down menu for a form

  24. 24

    Remove underline on Spinner Drop down and Change the color of the triangle

  25. 25

    How do I stop links from wrapping onto multiple lines in a CSS drop down menu?

  26. 26

    How can I sort a particular column when triggered by a drop down menu on the client side?

  27. 27

    How do I switch the direction of the menu drop down when it flows over the width?

  28. 28

    how to customize the spinner in android?

  29. 29

    Drop down menu for posts as like twitter and facebook implemented

ホットタグ

アーカイブ