Steve
Steve

Reputation: 1459

Android: how to have two separate layouts

there are lots of layout questions when I search here, but I just can't find something to address this - and its possibly because I am going about it the wrong way, or perhaps using the wrong terms to describe what I am trying to achieve. I apologise in advance if this is answered elsewhere, but would appreciate some pointers.

What I want to do is have a screen for a login task, then once logged in I go to another screen to do some other stuff (and this screen will have a totally different layout).

The question I have is: Can I have two separate layouts (ie: main.xml and login.xml), and depending where I am in the code, just change them? And if so, how?

I did try adding setContentView(R.layout.login); in the code, but it just seems to pick the last one set and stick with it regardless of where in the code you are. I also tried breaking the login part into another class then use startActivity(new Intent(this, Login.class)); to call it, but its almost like it ran in parallel to the main app, since things in the main app kept going even before the login was completed.

ie: in the main activity:

startActivity(new Intent(this, Login.class));
Toast.makeText(this, "This is main", Toast.LENGTH_LONG).show();

Then I get the login screen pop up, with the toast message popping up over it - and the main activity just carries on regardless of what I do in the login activity. The login activity does not just end either - do I need to kill it, or do a System.exit() on it?

I'm hoping its something simple I am doing wrong, I'm kind of trying to kludge ideas from various websites and the two textbooks I have, but nothing seems to be working.

Any help or ideas will be greatly appreciated

Upvotes: 0

Views: 628

Answers (3)

Pedro Loureiro
Pedro Loureiro

Reputation: 11514

You have several options.

Maybe the better option is to use two different activities. When you start the second one (main activity), call activity.finish() in the first one (login activity).

Upvotes: 1

Dinesh Sharma
Dinesh Sharma

Reputation: 11571

I think what you want to do is launch a new screen when a user a logged in properly. just launch a new activity when the user is properly logged in. Take help from the following:

Intent i = new Intent(this,Login.class);      
        startActivityForResult(i, 1);

Again in the MainActivity use onActivityResult() method to receive the result if you want to perform some action back in the Main Actvity.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==1){
            Toast.makeText(this, "Pass", Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(this, "Fail", Toast.LENGTH_LONG).show();
        }
    }

Try this.

Upvotes: 2

Jim Blackler
Jim Blackler

Reputation: 23179

The correct way to do this is to have two activities MainActivity and LoginActivity that each have their own layout. You will get into back-button hell if you don't take that approach.

(Re. "things in the main app kept going even before the login was completed" you'll need to add more detail to explain what you were seeing, that doesn't sound right to me.)

Upvotes: 3

Related Questions