3

I am getting started with some android development, and on the first screen of the app I am working on, I want the screen to display either a login/registration prompt, or the User's photo, depending on whether or not they are logged in to the app.

Should I just scratch using the xml for this? or there a way I can get the xml to draw differently depending on whether or not the user has logged in?

wfbarksdale
  • 7,498
  • 15
  • 65
  • 88

5 Answers5

4

It's actually a good idea to keep your layouts in XML. Just create two layout files (one for each screen), and programmatically select one or the other based on login status.

public class MyActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate();
        /* Check login status */
        if (loggedIn == true){
            this.setContentView(R.layout.logged_in);
        }            

        else if (loggedIn == false){
            this.setContentView(R.layout.not_logged_in);
        }
    }
}

You might want to try the Android tutorials to get started. Or if you want some more depth, try the Dev guide

*Edit oops, forgot to call super.onCreate()

sgarman's suggestion to create a separate login activity that automatically forward the user to your main activity is also quite viable, and he makes a good point regarding the modularity of that approach.

You could have an activity just for logging in, if the user is already logged in you can finish() that activity and route them (startActivity()) to a logged in activity with your layout.

IMO that's mostly a personal preference.

Chris Bye
  • 1,028
  • 7
  • 17
1

There are a few ways to handle this.

You could have an activity just for logging in, if the user is already logged in you can finish() that activity and route them (startActivity()) to a logged in activity with your layout.

You could include all the layout parts you need on top of each other using RelativeLayouts and setting the user info section to visibility:gone. If the user is logged in you can findViewById() and setVisibitlity to View.VISIBLE.

You could check to see if the user is logged in BEFORE you call setContentView, and choose the correct layout based on that.

The first choice is probably the most modular but also the most heavy lifting. In my code I have a log in activity that returns results to other activities based on successful login etc. This way the logic is totally separate and I can just make one call from anywhere in the app to handle login issues.

sgarman
  • 6,152
  • 5
  • 40
  • 44
0

I have the same constellation in the app I'm developing at the moment.

First of all you need a launching Activity extending FragmantActivity or AppCompatActivity. Let's call this activity MainActivity. Just create another fragment for the login/registration with its layout. And in the MainActivity, which is the launching activity, you can handle in the onResume()-Method, whether to show the login fragment or not.

Example

@Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        if (!userManager.isLoggedIn()) {
            LoginFragment loginFragment = LoginFragment.newInstance();
            //LoginFragment loginFragment = new LoginFragment();
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_layout, loginFragment,
                            LoginFragment.class.getSimpleName()
                    ).commit();
        }
    }

userManager is an object of a service class for authentication. It has a method returning a boolean indication whether the user is logged in or not. If the user is logged in, the current fragment is replaced by the login fragment. That's all.

With fragments you are able to display different layouts conditionally with its own logic.

Explanation of instantiating the fragment:

If you add a fragment to your project, AndroidStudio creates some boilerplate code with a static factory method newInstance(), because the constructor mustn't be overloaded. If you don't need to pass arguments for the creation of the login fragment you can do it with new LoginFragment(). But if you need arguments, you should use the factory method newInstance, which you instantiate the fragment without any arguments. Here is a good explanation for that: https://stackoverflow.com/a/9245510/474985

Sedat Kilinc
  • 2,843
  • 1
  • 22
  • 20
0

You have two choices. 1) Make two separate screens and show the appropriate one. 2) Use a FrameLayout in your XML and overlay the two different sets of views on top of each other. Then in code set the visibility to gone for the stuff you don't want to see.

w.donahue
  • 10,790
  • 13
  • 56
  • 78
0

You can use setContentView method in onCreate. Define a member variable to check user logged. And choose XML layouts using setContentView methods by each cases.

public class Login_testActivity extends Activity {

//If logged, set user id.
private String loginId = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(loginId==null || loginId.equals(""))
    {
        //Not Logged. show login form.
        setContentView(R.layout.login);
    }
    else
    {
        //Logged. show user's photo.
        setContentView(R.layout.main);
    }
}
IvoryCirrus
  • 622
  • 1
  • 4
  • 15