Home > Software design >  How can I programmatically change include lay-out for java android?
How can I programmatically change include lay-out for java android?

Time:05-24

How can I programmatically change layout="@layout/keyboard" with another lay-out layout="@layout/play_again". Or is there a better way of doing this?

<include
        android:id="@ id/includeKeyboard"
        layout="@layout/keyboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginBottom="5dp"
        android:layout_weight="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

lay-out play again

lay-out keyboard

CodePudding user response:

You use fragments. Make a fragment for both of the included layouts. Then you can swap out which fragment is being displayed via a fragment transaction.

CodePudding user response:

In my opinion there is no way to do that, must just have something like this

<include
        android:id="@ id/includeKeyboard1"
        layout="@layout/keyboard"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
<include
        android:id="@ id/includeKeyboard2"
        layout="@layout/keyboard2"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

Then in your code you can do

View includeKeyboard1 = findViewById(R.id.includeKeyboard1);
View includeKeyboard2 = findViewById(R.id.includeKeyboard2);

if(someConditions) {
    includeKeyboard1.setVisibility(View.VISIBLE);
    includeKeyboard2.setVisibility(View.GONE);
} else {
   includeKeyboard1.setVisibility(View.GONE);
   includeKeyboard2.setVisibility(View.VISIBLE);
}
  • Related