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 .
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.
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.
Related
I have 2 layouts which contain the same buttons
layout_1.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
and
layout_2.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Please assume these are all valid layouts etc.(I am just adding the relevant code.).
So in my fragment ,I inflate and use layout_1.xml in onCreateView.I want to toggle between the 2 scenes using button_1.
I can set the listener for button_1 in layout_1.xml during the onCreateView().
The problem is trying to set a listener on that button in the second view.i.e. the listener does not activate for the second scene(with layout_2.xml).And hence i canot toggle between the 2 scenes.Is there a way to achieve this?
It would actually appear that a proper way to do this would be to on the second scene you define an action to be performed as such:
mSecondScene.setEnterAction(new Runnable() {
#Override
public void run() {
((Button) mSecondScene.getSceneRoot().findViewById(R.id. button_1)).setOnClickListener( ... );
}
This will allow you to set your ClickListener on the View without the data binding to a generic click listener method. Then you can perform the Transition to the second scene and viola.
In general, it is not a good idea to have multiple views with the same id. This is what caused the confusion here.
Note: Below is the solution used by OP that was suitable for their specific needs:
One simple solution is to use the onClick attribute in the XML file. You can assign the same onClick method to multiple items. Like this:
And in your activity.java add this:
public void buttonClicked(View v){
Log.d("TAG","Button clicked!!"
// do stuff here
}
2nd option:
When you set a listener for one button with the id of button_1, it does not set the listener for both buttons, it only sets it for the first one. If you want to set the same listener for both, all you need to do is to assign these button different ids and then assign them the same listener.
This is what you should do:
Listener myListener = new Listener(){.. blah blah....};
((Button) findViewById(R.id.some_id)).setListerner(myListener);
((Button) findViewById(R.id.some_other_id)).setListerner(myListener);
3rd option:
findViewById(R.id.id_of_layout1).findViewById(R.id.button_1)
findViewById(R.id.id_of_layout2).findViewById(R.id.button_1)
in this case, you need add some id to your layout files, for example: layout_1.xml:
<RelativeLayout
android:id="+id/id_of_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I've a scenario where users take a quiz that has 40+ question. Creating 40+ activities is tedious task & i want to know is there anything exists to reuse?
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Who founded Apple?"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="49dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:text="Fill answers here"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="37dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/editText"
android:layout_toEndOf="#+id/editText"
android:clickable="true" />
</RelativeLayout>
In my MainActivity.class, im normally doing like
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// ....Go to next activity through Intent...
}
});
Look inside onClick, I need to have each activity for each question that is not good.
Any other solution?
You probably want to create a data structure for your quizes if you haven't. Once you have a datastructure you can think about what kind of adapter you want to use: that is, the mechanism that will link your set of quizes to the UI. For instance, when needing to display a list of something, listview is used with an ArrayAdapter (or BaseAdapter for more custom styled listview).
In the support libraries there is something called a viewpager, which is a set of pages you can swipe horizontally between. You might consider using this.
Or you can create your own view class that has all the views you need to display the quiz, add a method like switchQuiz(Quiz q) which changes the ui with the new quiz information, and use this view in a single activity. Specifically you can take your activity_main.xml layout file, and instead of using it as the layout of an activity, make a custom view (public class QuizView extends RelativeLayout { }) that has a a way to update the ui for the switch quiz functionality. Read here for more information of how to do this: http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/
Create two buttons, prev and next..
prev take you to prev question and next take you next question
Create a class question containing following variable
int number_of_option;
String[] options;
String question;
String answer;
Create a class quiz containing ArrayList<question> list;
add required method to add question, get question , update answer..etc..etc
Create variable current_position_in_UI in quiz class to display that question in activity.
On next/prev button click call a method of quiz to get question fields. something like list.get(position) and update your views with those values on that call. Also update your current_position_in_UI. Hope it helps :)
I need tab like above picture.if i choose Address it should show existing address tab.if i choose New Address it should show address form .but tabs should be in particular area of the activity.Is this possible in android?
If you want to create your layout(View) in activity
1)Try use this xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- ADD YOUR FIRST VIEW ie YOUR SELECT SERVICE DROP DOWN -->
<!-- ADD YOUR SECOND VIEW ie OF VISITING CHARGE 250 APPLICABLE -->
<!-- LAYOUT FOR YOUR TAB -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<Button
android:id="#+id/addressButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/addressActiveBtn"
android:text="address" />
<Button
android:id="#+id/newAddressButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/addressDeActiveBtn"
android:text="new address" />
</LinearLayout>
<!-- CREATE ADDRESS SCREEN LAYOUT AND INCLUDE HERE -->
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/addressScreen"
android:visibility="visible" />
<!-- CREATE NEW ADDRESS SCREEN LAYOUT AND INCLUDE HERE -->
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/newAddressScreen"
android:visibility="gone" />
</LinearLayout>
2)You need to handle the Layout views(Address and New Address) by code.
ie you need to implement onClickListener on your tab buttons and handle
switching of layout views of Address and NewAddress
How:
addressButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
addressLinearLayout.setVisibility(View.VISIBLE);
newAddressLinearLayout.setVisibility(View.GONE);
}
});
newAddressButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
addressLinearLayout.setVisibility(View.GONE);
newAddressLinearLayout.setVisibility(View.VISIBLE);
}
});
Hope this helps you somehow....
I don't think it's possible to use Android's navigation tabs like that.
However, what I would do is create two buttons and two fragment. When clicking on the first button the fragment with the address would be shown, and when clicking the second button the form would be displayed.
You don't even have to use fragments, you could just hide the current address or the form visually with visibility.GONE, depending on which button is pressed, but I think using fragments is best practice.
I think tabs use it like that would not be a good implementation in accordance to Android design gidelines. Instead, you could use two buttons that enable a fragment or view visibility. To manage the visibility behavior use the ViewStub class. Here is an example that show you how ViewStub works.
ViewStubExample
I have created an activity in which there is four button in center . i want that when i click on any of button then there should be open another Xml file in same. i don't want to move on another activity . i want to show on same Activity with new Xml. This new Xml hide previous one.
first xml is:-
<include
android:id="#+id/header"
layout="#layout/header"
android:layout_alignParentTop="true">
</include>
<Button
style="#style/homePageBtnStyle"
android:id="#+id/howToUse"
android:layout_centerHorizontal="true"
android:layout_marginTop="110dp"
android:text="#string/howToUse"
/>
<Button
style="#style/homePageBtnStyle"
android:id="#+id/purposeOfApp"
android:layout_alignLeft="#+id/howToUse"
android:layout_marginTop="10dp"
android:text="#string/purposeOfApp"
android:layout_below="#+id/howToUse"
/>
<Button
style="#style/homePageBtnStyle"
android:id="#+id/rateThisApp"
android:layout_alignLeft="#+id/purposeOfApp"
android:layout_marginTop="10dp"
android:text="#string/rateThisApp"
android:layout_below="#+id/purposeOfApp"
/>
and second xml is:-
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:text="#string/howtousetext"
android:id="#+id/textInstruction"
android:textColor="#color/btn_border"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:
android:background="#color/themeColor"
/>
Best thing you can do is to use ViewFlipper. You can include multiple layouts in a single ViewFlipper and you can navigate from one layout to another using showNext() and showPrevious().
And since you have only two view, this becomes very simple.
call setContentView with the second xml id again and call invalidate().
try this one:
Button button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Test1Activity.this.setContentView(R.layout.main2);
}});
I have two layouts, what is the best way to switch between the two layouts when a user clicks on a button?
You could call setContentView(R.layout.layout2) on ButtonClick
The best way is to use 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);
// you can switch between next and previous layout and display it
viewFlipper.showNext();
viewFlipper.showPrevious();
// 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 tree 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/secondLayout"
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>
Use ViewSwitcher.
make one layout file that includes two layouts. your two layouts should be place in viewswitcher.
associate an onclick listener that switch two layout with a button.
if you separate two layouts in different file, you can use tag in layout xml file.
Use "fragment manager" after creating fragments and putting your layouts into it on run time or "view pager" as it can also add swapping effect. Do not use setContentView(R.layout.your_layout) without clearing the previous layout (use "gone" or "clear") for changing layout on run time as it slows down your app (because now there are two layout running) and even creates confusion for the app.