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>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Maff
  • 441
  • 1
  • 6
  • 27
  • No, that's not directly possible. I would suggest visibility change inside your included layout. – Jeel Vankhede Sep 21 '19 at 10:32
  • 1
    Possible duplicate of [How can I programmatically include layout in Android?](https://stackoverflow.com/questions/18999601/how-can-i-programmatically-include-layout-in-android) – Abhinav Suman Sep 21 '19 at 10:34

2 Answers2

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

A S M Sayem
  • 2,010
  • 2
  • 21
  • 28
  • 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 – Maff Sep 21 '19 at 14:25
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

Black mamba
  • 462
  • 4
  • 15