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:
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With