mongeki
mongeki

Reputation: 11

How to programmatically change included layout? Android Studio, Scrolling Activity

I have activity_scrolling.xml, content_scrolling.xml, one_more_content.xml In activity_scrolling.xml:

<include="@layout/content_scrolling" />

How can I change the included layout to one_more_content.xml programmaticaly in ScrollingActivity.java?

Upvotes: 1

Views: 124

Answers (1)

Gagan Batra
Gagan Batra

Reputation: 393

You can use ViewFlipper for this

<ViewFlipper
    android:id="@+id/viewFlipper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <include android:id="@+id/view1" layout="@layout/content_scrolling" />
    <include android:id="@+id/view2" layout="@layout/one_more_content" />

</ViewFlipper>

and then update the UI using this

binding.viewFlipper.setDisplayedChild(1);

If you are not using view binding, use this instead

ViewFlipper vFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
vFlipper.setDisplayedChild(1);

Upvotes: 3

Related Questions