Like overflow menu overlaps the toolbar same way spinner overlaps itself when it is shown drop down item in lollipop and above android version. So I need to place it below the spinner view not over it.
You can use
android:overlapAnchor="false"
This will show the dropdown below the spinner view (Work on api level 21 and higher).
For all api use
android:dropDownVerticalOffset="35dp"
or whatever value that suits your needs.
android:spinnerMode="dropdown"
android:dropDownVerticalOffset="50dp"
This will show the dropdown below the spinner view.
Recently, I faced the same issue but I have several spinner across the app and I wanted it to look the same without having to add the same property in all of them, so I used the style.xml to customize my spinners as below
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:spinnerStyle">@style/spinner_style</item>
</style>
<style name="spinner_style" parent="Widget.AppCompat.Spinner">
<item name="android:dropDownVerticalOffset">40dip</item>
<item name="overlapAnchor">false</item>
<!--Other customizations-->
</style>
</resources>
Ues the following attribute in Spinner
android:dropDownVerticalOffset="35dp"
Here is the below is code for Spinner
<RelativeLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="3dp"
android:layout_weight=".28"
android:orientation="horizontal">
<Spinner
android:id="@+id/spinner_users"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:layout_marginLeft="5dp"
android:background="@android:color/transparent"
android:dropDownVerticalOffset="35dp"
android:spinnerMode="dropdown" />
<ImageView
android:layout_width="30dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:src="@drawable/drop_down" />
</RelativeLayout>
It depends on where your spinner is located and how many items in the drop down list.
If the spinner layout is at the top of screen, then android:overlapAnchor="false"
is enough;
If the spinner layout is around the center of screen with many items, e.g. 60 items, then android:overlapAnchor="false"
doesn't satisfy your requirement as no enough space for drop down list; and it's the similar situation when spinner sits at the bottom, but with drop down list above spinner.
I think Spinner isn't a flexible android widget as not many settings available, e.g. drop down height, the first item will be selected when loading data. To achieve the same thing, I think ListPopupWindow
is a good replacement. Here is a simple example of it:
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical" android:gravity="center">
<TextView
android:gravity="center_vertical"
android:text="Tap for more options"
android:id="@+id/tv_anchor"
android:layout_margin="20dp"
android:background="@drawable/spinner_bg"
android:layout_width="match_parent"
android:layout_height="40dp"/>
</LinearLayout>
activity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.act_drop_down)
tvAnchor = findViewById(R.id.tv_anchor)
val popupList = ListPopupWindow(this)
popupList.setAdapter(ArrayAdapter(this, android.R.layout.simple_spinner_item, getData()))
popupList.anchorView = tvAnchor
popupList.width = AbsListView.LayoutParams.WRAP_CONTENT
popupList.height = 100*3
popupList.setOnItemClickListener { parent, view, position, id ->
popupList.dismiss()
}
tvAnchor.setOnClickListener {
popupList.show()
}
}
© 2022 - 2024 — McMap. All rights reserved.