How to alternate between 2 layouts in the same activity? - android

I want to know how to get the same activity to show 2 layouts. I explain, I have an activity with a login, and within this same are 2 buttons (one called "login" and another "SingUp") the idea is that when you click on one of these 2 shows a different layout for each button, but without having to change all activity in one. I achieved it partially by adding the SetContentView to the Onclic of each button, and it works but only the first time, without the activity it starts with the login layout and then I click on the singUp button, if it changes layout but if later I want to return to the login layout I can not anymore I have to restart the app.What I want to achieve is to change layout in the same activity using buttons, whatever method they mention or they help me to know which part of my code is wrong :(Thank you.
public class MainActivity extends AppCompatActivity {
private Button btnLogin,btnSignup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnSignup=(Button)findViewById(R.id.btnSignup);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_main);
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_sign_up);
}
});
}
}
with the previous code and managed to alternate between 2 layouts in the same activity, but the buttons only work the first time if I try to go to a layout by pressing a button for the second time, this simply does not work until I restart the app.

Option 1
Try recreating the Activity. Save which view to load then recreate.
private Button btnLogin,btnSignup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(preference.getInt(ACTIVITY)==1)
setContentView(R.layout.activity_main);
else
setContentView(R.layout.activity_sign_up);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnSignup=(Button)findViewById(R.id.btnSignup);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
preference.setInt(ACTIVITY, 1);
recreate();
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
preference.setInt(ACTIVITY, 2);
recreate();
}
});
}
Option 2
Use fragments. Create 2 fragments and inflate them into activity on onclick of button
Option 3
Use VISIBLE/GONE with predefined layouts inside activity_main

Create one layout with two separate views that match_parent's height and width.
Hide / show each one when switching views
<LinearLayout
android:id="#+id/parent"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/main"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical"
android:visibility="visibile">
<!-- Add child views here -->
</LinearLayout>
<LinearLayout
android:id="#+id/sign_up"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<!-- Add child views here -->
</LinearLayout>
</LinearLayout>
Then programmatically...
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainView.setVisibility(View.VISIBLE);
loginView.setVisibility(View.GONE);
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainView.setVisibility(View.GONE);
loginView.setVisibility(View.VISIBLE);
}
});
You can approach it in other ways using Fragments, which one could argue that's the more appropriate way.
Switch between two fragments

Related

Android: Handle Onclick Listerner in included layout [duplicate]

I have two java class and two layout for both the class.
Each layout is having one button in it.
Both classes are extending Activity.
Now in first layout I used include tag like this
<include
android:id="#+id/clicked"
layout="#layout/activity_main" />
I can now see two buttons but the second button is not working.
First You have to declare and initialise the include view and then decalre and initialise both buttons using view.findViewById() method as follows:
View includeView = (View)findViewById(R.id.clicked);
Button button1 = (Button)includeView.findViewById(R.id.button1ID); //decalre button like this
Button button2 = (Button)includeView.findViewById(R.id.button2ID);
And then set their onClickListeners
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
** EDIT **
Fixed the typo. Should be includeView on the findViewById.
Good explanation though!

Change between layouts / many xml files or all layouts in same xml file?

Writing a simple program that has 2 windows that switch from one to the other at a button press. At first I managed to do this with two layout files in the same XML and just hide one and show the other, but it was near impossible to make a UI this way since the preview window would just bug out and not show what i added to the bottom layer ( pic related: ) http://i.imgur.com/7RsP9t7.png
So I thought instead, I would make 2 XMLS and have 1 layout in each and now it was easy to make a UI but the code on the other hand does not work.
java.lang.NullPointerException # btn2.setOnClickListener(new View.OnClickListener() which doesnt seem to unlogical, guess u cant listen to a button that isnt active or?
public class MyActivity extends Activity {
Button btn1, btn2;
LinearLayout layHome, layAddNumer;
RelativeLayout layAddNumber ;
EditText ph0ne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
btn1= (Button) findViewById(R.id.buttonGoTonumber);
btn2= (Button) findViewById(R.id.buttonAddNumber);
layHome = (LinearLayout) findViewById(R.id.layHomeddddddd);
layAddNumber = (RelativeLayout) findViewById(R.id.layyPhone);
ph0ne = (EditText) findViewById(R.id.phoneNumberText);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.phonelayout);
//layHome.setVisibility(View.GONE);
//layAddNumber.setVisibility(View.VISIBLE);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_my);
}
});
xml: This site does not let me add XML files. But btn1, LinearLayout layHome is in activity:my while Button btn2, RelativeLayout layAddNumber and ph0ne is in my second XML file

Detecting Single Tap in Screen for Android

i want to add a code where when a user tap the screen in his/her screen in android, there will be some dialog box showing that the screen is tapped. i tried few codings but it makes my project crash. any suggestions for coding? thank you.
Implement on the root view an OnClickListener interface.
//TestActivity
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
findViewById(R.id.ll_root).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showDialog();
}
});
}
private void showDialog() {
new AlertDialog.Builder(this)
.setTitle("Screen tapped")
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}
//Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/ll_root"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Use onInterceptTouchEvent() method.
From documentation
Implement this method to intercept all touch screen motion events.
This allows you to watch events as they are dispatched to your children,
and take ownership of the current gesture at any point.
Example:

my activity consist of one activity and many hidden layouts. on different button click respective layouts are set to visible

Now my question is this, is there any way to apply any default animation when each of the layout is set to visible? Instead of appearing all of a sudden, any sliding animation would look sleek.
//to show home page
homeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
liMenu.setVisibility(View.VISIBLE);
}
});
//to show .net page
dotnetButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
liDotnet.setVisibility(View.VISIBLE);
}
});

I want to start another activity when it is clicked?

Well I have an activity which contains nothing but a relative layout with brown background,
and I want to start another activity if the users clicks anywhere on the screen, how would i do that ??
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
}
First, give ID to your RelativeLayout by putting android:id="#+id/relativeLayout" in your layout file then.
RelativeLayout l = (RelativeLayout) findViewById(R.id.relativeLayout1);
l.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// startActivity here
}
});
or without using your RelativeLayout just implement the onTouchEvent(MotionEvent) of activity
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
// start activity
}
return super.onTouchEvent(event);
}
Note: Not yet tried this code.
Add onClick attribute to your layout xml, and implement onClick method in your activity to start a new activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick"
android:orientation="vertical" >
In your activity add an onClick method.
public void OnClick(View v) {
startActivity(new Intent(this,MyNewActivity.class));
}
I think your Mainscreen has Parent layout either Linear,Relative,Frame. Take reference of that and handle click listener.
Ex:
If your Parent layout is Linear.
LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
linear.setOnClickListener(this);

Categories

Resources