Search code examples
androidauthenticationnavigation-drawerslidingdrawer

alternate option in navigation drawer, if option A is selected, option B should be displayed in drawer list and vice versa


I'm using navigation drawer. Navigation Drawer contains elements/items (Like: User Profile, Login/Logout)in it.

So when user clicks on Login/Logout option, it will navigate the user to a login page where User will login via Google Account. Hence, when user is logged into the app, the option should change from login to logout and when user decides to logout,vice versa.

Furthermore, when user first access into the app, the option should be displayed as "Login". How is it possibly done?

Hence at this point in time, the tab is showing "login" even when user is not logged into the account.

Code:

public void initializeMenu() {
    ChannelAppMenuAdapter mAdapter = new ChannelAppMenuAdapter(this);

    // Profile
    mAdapter.addHeader("Account");
    //Case 2:
    /*DAPO:DEV02-20150106: Condition to check if user is login, user is login, title will display LogOut, else
     * will display "Login"*
     */
    if (isLogin==true)
        title = "logout";
    else
        title="login";
    icon = R.drawable.google_icon;
    mAdapter.addItem(title, icon);
}

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        switch (position) {
        case 2:
            //DEV02-20141231: alternation of login/logout options, login to change to logout when user is login and vice versa
            Intent intent = new Intent(getApplicationContext(),
                    ChannelAppLoginInfoMainActivity.class);
            startActivity(intent);
            break;
}

Solution

  • The logic which invloves checking login and logout state, save the isLogin as static global variable.

    For example create a class. Call it as RunTimeData.

    Declare a boolean variable in that class as follows.

    public class RunTimeData{
        public static boolean isLogin;
    }
    

    In your Login/Logout logic set this variable value as follows.

    RunTimeData.isLogin=true; or RunTimeData.isLogin=false;
    

    In your getView() method of ChannelAppMenuAdapter class, you need to check for this boolean value as follows.

    TextView textView=findViewById(...);
    if(RunTimeData.isLogin){
        textView.setText("Logout");
    else
        textView.setText("Login");
    

    Hope this helps.