0

Instead of moving views in a current layout, I was wondering if i could instead load a different layout whilst the program is running.

For example in the on create i would use:

setContentView(R.layout.layout1);

and then in an on click listener i would use:

setContentView(R.layout.layout2);

I say this since I am using a custom dialogue which prevents me from producing a dialog to overwrite it. I have attempted it but so far have only received errors. I would really like to know if this is possible.

  • It is but not often a good idea, IMHO, and can get messy. You are better off using fragments, switching activities, or taking some other action. If you tell us exactly what you are trying to accomplish and why your previous tries haven't worked then we might be able to suggest a better way. – codeMagic Jul 01 '14 at 23:12
  • I am using a custom dialog to send information off. However, when an error is caused, i want to show a message saying error. However, a custom dialog does not allow another dialog to be created. Additionally, i thought changing the views around in the current layout would take more time than if i had changed the layouts around. PS: Since it is a contained part of my application, do you think i could get away with changing the layout even though it is inefficient. – jamesgates1 Jul 01 '14 at 23:16
  • One thought is that you can use another activity with a Dialog theme, if that would work for you, instead of the dialog you have. That would allow you to create your error dialog. Not sure about the design of that. Yes, you definitely could change the layout but I just don't know that's the best approach. You mean change the layout of your dialog or activity? – codeMagic Jul 01 '14 at 23:18
  • Change the layout of dialog. – jamesgates1 Jul 01 '14 at 23:20
  • [See if this answer](http://stackoverflow.com/questions/16071500/is-it-possible-to-keep-a-dialog-open-after-clicking-the-neutral-button/16071673#16071673) would give you what you need. You could then open your "Error Dialog" in the dialog themed activity. – codeMagic Jul 01 '14 at 23:23

1 Answers1

0

This is possible, but risky and not recommended for a final product. The problem is, you cannot access already defined views once you have switched views. You need to assign them all again for the new layout. So once you switch the content view, re-initialize all of your views and anything that references the old layout. Calling a view from the old layout will cause a crash or error message.

Like CodeMagic said, it is best to use fragments and the FragmentManager for this. And really, this is not a stable way to produce good code. I recommend using separate fragments and using backstacks and such so that not only will your game work, but you can easily navigate back to the original fragment, rather than use makeshift code that may barely work.

After setContentView(R.id.yourLayout) is called, you need to re-instantiate all of your other views. So like say you used an ImageView view to show the color changes, well you need to instantiate that ImageView after you setContentView(R.id.yourLayout) so that it pulls from the new layout, and does not reference to the original layout.

Example:

ImageView imageView;

public void onCreate(Bundle b){
    super.savedInstanceState(b);
    setContentView(originalLayout);
    //Instatiate all of your original Views.
    imageView = (ImageView) R.id.yourImageView;
}


public void onButtonClick(View){
    setContentView(newLayout);
    imageView = (ImageView) R.id.yourNewImageView;
    //All other views
}

If you need an example of the fragment manager solution go here: https://developer.android.com/training/basics/fragments/creating.html and look through some of their examples on how to properly do what you are trying to do.

TheJavaCoder16
  • 591
  • 4
  • 14
  • Since I would not need to switch back as the user would have to exit the dialog, could you provide a code example? – jamesgates1 Jul 01 '14 at 23:28