Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preview ViewSwitcher in AndroidStudio

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?

like image 711
JustinHK Avatar asked May 06 '15 17:05

JustinHK


2 Answers

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.

like image 78
JJ86 Avatar answered Sep 25 '22 01:09

JJ86


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>
like image 33
Rahul Avatar answered Sep 26 '22 01:09

Rahul