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?
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.
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
-
First Screen - my_product.xml - this layout will define something about product.
Second Screen - about_us.xml - this layout will describe about your company.
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>
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