Reputation: 439
I am writing a simple game and I'm wanting the main screen to have a choice of 3 layouts, for 2 handed, right handed or left handed.
I have an include
for the controls. However I'm struggling to get the layout
to change programatically. Been searching since last night but cannot find a way to do it, is it even possible?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GamePlay">
<include
android:layout_width="match_parent"
android:layout_height="88dp"
layout="@layout/hand_two" <!-- this is what needs to change depending on settings -->
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/handLayout"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 0
Views: 1638
Reputation: 2080
From the animusmind's answer: Use a ViewStub instead of include:
<ViewStub
android:id="@+id/layout_stub"
android:inflatedId="@+id/message_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.75" />
Then in code, get a reference to the stub, set its layout resource, and inflate it:
ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
stub.setLayoutResource(R.layout.whatever_layout_you_want);
View inflated = stub.inflate();
Answer collected from kcoppock
Upvotes: 2
Reputation: 472
If you have multiple views which you would like to load them on demand programatically, ViewStub is one of the solution
please visit like link viewStub for multiple view
Upvotes: 0