Reputation: 29805
How do you switch between layouts in an activity and populate fields based on layout currently used?
For example if there is a logic in a view to load a view:
if(category == 1){
setContentView(R.layout.layout1);
}else{
setContentView(R.layout.layout2);
TextView title = (TextView) findViewById(R.id.titleTV);
title.setText("myTitle");
}
If the else code is called the view never sets the title TextView to the String.
How do I accomplish conditionally going between the two views?
Upvotes: 2
Views: 1707
Reputation: 5183
In case you want to flip across difference Views
you can examine various possibilities, such as:
ViewPager
via the compatibility library (http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html)ViewFlipper
or ViewSwitcher
(http://developer.android.com/reference/android/widget/ViewFlipper.html, http://developer.android.com/reference/android/widget/ViewSwitcher.html)FrameLayout
(http://developer.android.com/reference/android/widget/FrameLayout.html)Hope this helps for now! In case you need anything specific please shoot it!
Upvotes: 1
Reputation: 25761
Another alternative would be to use Fragment
. The advantage over the choices dimitris mentioned is that each fragment has it's own lifecycle, so you can delegate all code related to each view to its fragment, and keep your main activity clean.
To do this, simply use a FrameLayout in the Activity as a placeholder for the fragments. Fragments can communicate with the activity by using the listener pattern.
Upvotes: 2