Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewSwitcher and layout height in Android

Tags:

I added a ViewSwitcher to a LinearLayout, which has two views of different height, However it seems the ViewSwitcher is occupying space of the biggest of the views, rather than arranging itself. Is this how it should be?

What to do in the other case? I was trying to create an accordion, where the title panel, when clicked grows in size

like image 547
Azlam Avatar asked Aug 24 '10 15:08

Azlam


People also ask

What is ViewSwitcher in android?

ViewSwitcher is a sub-class of ViewAnimator and is used for switching between views which bring different views to customers. It is an element of the transition widget which helps us to add transitions on the views. We can use that to animate a view on the screen.

What is View switcher?

In Android, ViewSwitcher is a sub class of ViewAnimator that is used for switching between views. It is an element of transition widget which helps us to add transitions on the views. It is mainly useful to animate a view on screen.


1 Answers

The default behavior of ViewSwitcher, as inherited by ViewAnimator, is to consider all child views on layout, which will result in the ViewSwitcher occupying the space of the largest child.

To change this, all you need to do is to set the MeasureAllChildren flag to false. This will make the layout pass ignore the child view that is currently hidden. Set this flag e.g. in the onCreate method of the activity, e.g.:

    ViewSwitcher switcher = (ViewSwitcher)findViewById(R.id.ViewSwitcher);     switcher.setMeasureAllChildren(false); 

XML example:

<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:id="@+id/viewSwitcher"         android:measureAllChildren="false"> </ViewSwitcher> 
like image 199
Thorstenvv Avatar answered Nov 17 '22 00:11

Thorstenvv