0

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>
2

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

not ideal having to use a stub as I cannot see it in the layout, but have gone this route. The above is in Java though, of which I know none. I think I have it working though, thanks
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.