1

I want to change layout theme for my app. I design two layouts for one activity e.g. activity_main and activity_main2 for MainActivity.java. Now user will press Switch button 1st layout may go disappear and 2nd layout may go appear on Screen. Is it possible if yes kindly tell me how? This is my 1st layout which is set as default layout

This is my 2nd layout which is I want to replace with default layout on click on Switch button

If i using setContentView(R.layout.activity_main); and replace with setContentView(R.layout.activity_main2); on button click layout will change but i did'nt get widget's ids of 2nd layout. If anyone know kindly tell me

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • each activity is associated with one layout, so either start a second activity or use fragments – a_local_nobody Jan 04 '21 at 09:16
  • I did by creating both layout within one xml file. I just add visibility ' android:visibility="gone" ' to 2nd layout design. Just like this you can show user layouts as you like to wish by setting visibility according to your action. – ahmad bajwa Jan 04 '21 at 09:20
  • If this work for you let me know ill post it as answer. – ahmad bajwa Jan 04 '21 at 09:21

1 Answers1

0

If use use ConstraintLayout 2.0 I can recommend the following options:

  1. Switch between ConstraintSets if you want to maintain the same set of view across different states and just re-arrange them based on a user's preference;
  2. Switch between Constraint States if the difference between two states is more significant.

If you don't want to rely on ConstraintLayout or don't use ConstraintLayout 2.0 then possible options were already listed in the comments section of your question.

Lingviston
  • 5,479
  • 5
  • 35
  • 67