Preview ViewSwitcher in AndroidStudio
Asked Answered
D

3

18

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?

Destructive answered 6/5, 2015 at 17:51 Comment(3)
Is there any special reason why you use ViewSwitcher? It's very limited. Use ViewPager insteadGasket
@auval, I'm only using ViewSwitcher to show a loading indicator while the rest of the activity is loading, so I don't need anything very complicated. It looks like ViewPager would be overkill.Rheims
ViewSwitcher is an overkill for showing a loading indicator. Use a simple view for thatGasket
C
7

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.

Claudine answered 27/7, 2017 at 13:56 Comment(0)
C
1

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>
Coenobite answered 31/7, 2017 at 19:15 Comment(2)
A ViewSwitcher can only have two child views. developer.android.com/reference/android/widget/…Avalos
@Avalos I don't think so, do you have any reference for that?Coenobite
P
0

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

Pressley answered 31/7, 2017 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.