Reputation: 159
I've have doubt about layouts and activities and the main concern is the efficiency of the app . My question is
Is it easy and efficient to use multiple layout XML files in a single activity like a single main activity and just change the view content of different XML files for example : Login and Registration layout files upon a single activity using handler
OR
Different activities , like for login page and registration page there are separate activities with corresponding layout files
Which is the best practice in terms of efficiency and easiness ? Also please list out the pros and cons of these approaches ? And situations to use any one of these approaches?
Thank you .
Upvotes: 1
Views: 4025
Reputation: 414
Method - 1 :
See this complete example of android.widget.ViewFlipper. With it you can create different layout from xml and then switch among them with simple method like this:
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);
// or you can switch selecting the layout that you want to display
viewFlipper.setDisplayedChild(1);
viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.secondLayout)
Xml example with two layouts:
<ViewFlipper
android:id="@+id/myViewFlipper"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
</LinearLayout>
<LinearLayout
android:id="@+id/thirdLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
</LinearLayout>
</ViewFlipper>
Method - 2:
Add ViewSwitcher
widget to your xml layout file. to the **ViewSwitcher**
add 2 new layouts
viewSwitcher = (ViewSwitcher)findViewById(R.id.viewSwitcher1);
myFirstView= findViewById(R.id.view1);
mySecondView = findViewById(R.id.view2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (viewSwitcher.getCurrentView() != myFirstView){
viewSwitcher.showPrevious();
} else if (viewSwitcher.getCurrentView() != mySecondView){
viewSwitcher.showNext();
}
}
});
Xml example with two layouts:
<ViewSwitcher
android:id="@+id/viewSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:inAnimation="@android:anim/slide_in_left" >
<LinearLayout
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text"
android:text="This is simplezdscsdc text"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text"
android:text="This issdsdsds simplezdscsdc text"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</ViewSwitcher>
Note :- ViewFlipper is best for multiple layout in single activity.
Upvotes: 1
Reputation: 2541
There are no any pros to use multiple layout XML files in a single activity if you are talking about different logic behind the scene or somthing like this. Login page has own purpose, data processing logic, sending data to server etc. And they differ from purpose, logic, endpoints etc in registration page.
This is contrary to the Single responsibility principle of SOLID and is unacceptable. I think, the only situation that allows this is learning at the initial stage.
Upvotes: 0