Dynamically load layout in Android

My application requires 2 screens, and for this I created two different XML layout XML using RelativeLayout . One layout file is loaded whenever I start my activity. Now I want to load the second layout onto the same Activity , when the user clicks a button in OptionsMenu , and also when the user clicks the "Back" button, the first screen loads, but does not exit the application. So I do not need to create another Intent in my application.

0
source share
3 answers

Ideally, your application should have two different activities. You can add or remove a view component in a view, but if you are looking for two completely different screens, I would suggest you switch to a new activity.

I understand what you mean by "and also when the user presses the Bakc button, the first screen loads, but does not exit the application"

If you do not want to show the first screen, just complete the () action.

+3
source

Have you tried re-invoking setContentView?

Or you can use ViewFlipper,

Good example here .

+3
source

I would suggest simpler means.

Put both layouts in single XML and show / remove them accordingly. I don't think you need something more complicated :-)

+1
source

All Articles