Kishore Jethava
Kishore Jethava

Reputation: 6834

spinner dropdown start from top of spinner

Problem is Spinner dropdown list start from top line of Spinner but it should be start from bottom line of Spinner

Normal state

enter image description here

After Click

enter image description here

Spinner xml code

<Spinner
    android:id="@+id/spnSelectLanguage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:layout_marginTop="16dp"
    android:spinnerMode="dropdown"
    android:background="@drawable/spn_lang_dropdown_selector"
    android:gravity="center_vertical"
    android:popupBackground="#EAEAEA"
    android:textColor="#54a4db" />

Code

Spinner spnSelectLanguage = (Spinner)rootView.findViewById(R.id.spnSelectLanguage);
    ArrayAdapter<String> adapterLanguage= new ArrayAdapter<String> (context,R.layout.layout_lng_spinner_item,new String[]{"English","Arabic"});
    adapterLanguage.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spnSelectLanguage.setAdapter(adapterLanguage);

Style.xml

<style name="SpinnerStyle" parent="android:style/Widget.ListView.DropDown">
    <item name="android:divider">@color/grey</item>
    <item name="android:dividerHeight">1dp</item>
</style>

Upvotes: 14

Views: 13799

Answers (3)

iamreptar
iamreptar

Reputation: 1451

In your spinner:

android:overlapAnchor="false"

NOTE If you are supporting API below 21, this attribute has to be copy and pasted as it is available for API >= 21.

Upvotes: 23

Eugene Babich
Eugene Babich

Reputation: 1699

For me worked only when I created spinner style and added the "android:spinnerStyle" item to style of my theme.

I opened my /res/values/styles.xml, found my theme ("AppTheme") and added <item name="android:spinnerStyle">@style/custom_spinner</item> like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>            
    <item name="android:spinnerStyle">@style/custom_spinner</item>
</style>

<style name="custom_spinner" parent="@android:style/Widget.Holo.Light.Spinner">
</style>

Upvotes: 0

AliMola
AliMola

Reputation: 578

You can to set this lines in your Spinner xml code:

android:spinnerMode="dropdown"
android:dropDownVerticalOffset="50dp"

With this, your dropdownview will start with top offset. You can hardcode the offset number or calculate it at runtime and use:

setDropDownVerticalOffset(int pixels)

More in: Android Spinner

Upvotes: 16

Related Questions