Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Prevent different spinner heights (empty and filled)

I have some spinners on the layout and use loader to load their data from db in background. The problems is that empty spinners have smaller height until the data are loaded. So the layout jump.

How can I prevent this jumping?

Edit (added spinner row layouts)

Spinner row:

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Text1"
        style="@style/StyleSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

Spinner drop down row:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/style_spinner_row_padding" >

    <TextView
        android:id="@+id/Text1"
        style="@style/StyleSpinnerDropDownRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/Text2"
        style="@style/StyleSpinnerDropDownRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/Text1" />

</RelativeLayout>

Assigned styles:

<!-- // Spinner // -->
<style name="StyleSpinner" parent="AppTheme">
    <item name="android:textStyle">bold</item>
    <item name="android:gravity">start</item>
    <item name="android:textSize">@dimen/default_text_size_normal</item>
    <item name="android:padding">@dimen/style_spinner_row_padding</item>
</style>

<style name="StyleSpinnerDropDownRow1" parent="AppTheme">
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">@dimen/default_text_size_normal</item>
</style>

<style name="StyleSpinnerDropDownRow2" parent="AppTheme">
    <item name="android:textStyle">normal</item>
    <item name="android:textSize">@dimen/default_text_size_small</item>
</style>

Screen shots:

enter image description hereenter image description here

like image 927
WebDucer Avatar asked Feb 02 '14 16:02

WebDucer


People also ask

How can I limit the height of spinner dropdown view in Android?

You can also affect drop down view location and size by subclassing Spinner and overriding its getWindowVisibleDisplayFrame(Rect outRect) which is used by android. widget. PopupWindow for calculations. Just set outRect to limit the area where drop down view can be displayed.

What are two different ways to set the height and width of component in Android?

First get layout params using view. getLayoutParams(). Second then set the height and width value then last set the layout params of view. Save this answer.

What is set spinner show () method?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.


1 Answers

I've just had this same issue and managed to fix it for myself so here's what I did.

I am inflating android.R.layout.simple_spinner_dropdown_item in my spinner adapter for each item layout. When I looked at this layout I noticed it had no margins but just a set height which was coming from android:attr/dropdownListPreferredItemHeight.

So I thought I'd get this list preferred item height value programatically and set it as my spinners minimum height. Using the answer to this stackoverflow question I was able to get the correct value and set it to my spinner.

    android.util.TypedValue value = new android.util.TypedValue();
    getActivity().getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, value, true);
    android.util.DisplayMetrics metrics = new android.util.DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float ret = value.getDimension(metrics);

    mSpinner.setMinimumHeight((int) (ret - 1 * metrics.density));

I've removed 1dip because it seems that the returned value has been rounded up which makes it 1 pixel too high.

Now all my spinner are the same height, whether they have items or are empty.

like image 115
Steven Trigg Avatar answered Sep 20 '22 02:09

Steven Trigg