Preview ViewSwitcher in AndroidStudio

1.6k Views Asked by At

Is there an easy way to switch between the displayed view in a ViewSwitcher in the Android Studio preview, or is the only way to swap out the XML for the sub-views one at a time?

3

There are 3 best solutions below

0
JJ86 On BEST ANSWER

Unfortunately, there are no XML attributes or any option on Android Studio which can help you to define the displayed view.

A similar question for the ViewFlipper was asked here (they both are direct subclasses of ViewAnimator).

However, if and only if your views are large as the screen, you could use the include tag, for example:

<ViewSwitcher
    android:id="@+id/myViewSwitcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        layout="@layout/first_view">
    </include>

    <include
        layout="@layout/second_view">
    </include>

</ViewSwitcher>

Then you can see your layouts in a separate XML file.

0
Uddhav P. Gautam On

ViewPager can easily solve your issues.

ViewPager (it can hold multiple views). ViewPager is kind of Array Container for View objects. You can have ViewPager rotation (like you do the array rotation) or other techniques to swap inside views. And, you can create your each inside views based on the Factory DP, so that there happens less processing (shares common resources).

They have mentioned swiping views here (Note: you just need own view swiping techniques if you don't want to use default ViewPager rotation).

Creating swipes: https://developer.android.com/training/implementing-navigation/lateral.html

ViewPager for screen slides: https://developer.android.com/training/animation/screen-slide.html

2
Rahul On

First of all if you are thinking to use ViewSwitcher, just for showing ProgressDialog then you are not doing it in a way in which it should be. ViewSwitcher generally used to change layout of Activity. In your case ProgressDialog is not a View of your Activity rather it is just small helper which indicates some process is doing. So In short ViewSwitcher should be use somewhere where you want to alter complete screen of Activity.

In your case you can divide your layout into smaller layout files and group them using merge or include.

Create separate files for all different screens which will define UI of your Activity and group them using include.

For an example we can create small App for Introduction thing using ViewSwitcher -

  1. First Screen - my_product.xml - this layout will define something about product.

  2. Second Screen - about_us.xml - this layout will describe about your company.

  3. Third Screen - thank_you.xml - to say thank you to your users.

Group them in any container View.

<ViewSwitcher
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        layout="@layout/my_product"/>

    <include
        layout="@layout/about_us"/>

    <include
        layout="@layout/thank_you"/>

</ViewSwitcher>