2

Herewith I used 'spinner' with some styles. To set height of the spinner popup, I used 'List Popup Window'. It's working fine for more Android versions. But API 21 and 22 (Android 5.0 and 5.1) not supported. Below is my code that I found from How to limit the height of Spinner drop down view in Android.

MainActivity.java

Spinner newsSourceSpinner = (Spinner)findViewById(R.id.news_spinner);

try {
    Field popup = Spinner.class.getDeclaredField("mPopup");
    popup.setAccessible(true);

    // Get private mPopup member variable and try cast to ListPopupWindow
    android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(newsSourceSpinner);

    // set popup height
    popupWindow.setHeight(250);

} catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
    // silently fail...
}

activity_main.xml

<Spinner
       android:id="@+id/news_spinner"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:textSize="13sp"
       style="@style/Widget.AppCompat.Spinner.Underlined"
       android:theme="@style/SpinnerStyle"
       android:popupBackground="#80000000"
       android:focusableInTouchMode="true"
       android:focusable="true"/>

styles.xml

<style name="SpinnerStyle">
    <item name="colorControlNormal">@color/colorWhite</item>
</style>
alexrnov
  • 2,346
  • 3
  • 18
  • 34
Nuwan Withanage
  • 393
  • 7
  • 19

0 Answers0