Skip to content

Sign In Screen

Introduction

This screen allows users to sign in to both single-user and multiple-user mode mobile apps.

Sign In Screen Sign In User List Screen

Examples

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_multiple_user_sign_in)
    val settings = SignInWithActionBarScreenSettings().apply {
        showSwitchButton = sharedPreferences.getBoolean(KEY_MULTIPLE_USER_SHOW_SWITCH_BUTTON, true)
        showForgetPasscodeButton = sharedPreferences.getBoolean(KEY_MULTIPLE_USER_SHOW_FORGOT_BUTTON, true)
        screenTitle = sharedPreferences.getString(KEY_MULTIPLE_USER_SIGN_IN_TITLE, getString(R.string.basic_auth_title))
        signinButtonLabel = sharedPreferences.getString(KEY_MULTIPLE_USER_SIGN_IN_BUTTON_LABEL, getString(R.string.passcode_enter_button_text))
    }
    with(signInScreen) {
        initialize(settings)
        setUserName("Test User", "test_user@sap.com")
        showError(5)
        setForgetPasscodeClickListener {
            clearError()
            Toast.makeText(this@MultipleUserSignInScreenActivity, "Forgot Passcode? button clicked.", Toast.LENGTH_SHORT).show()
        }
        setSwitchAccountClickListener {
            switchToUserView(true)
            val users = mutableListOf<String>()
            1.rangeTo(20).forEach {
                users.add("Test user $it")
            }
            val adapter = CustomAdapter(users.toTypedArray())
            signInScreen.userListView.also {
                it.layoutManager = LinearLayoutManager(context)
                it.adapter = adapter
            }
        }
        setSignInClickListener {
            Toast.makeText(this@MultipleUserSignInScreenActivity, "Sign In button cliccked.", Toast.LENGTH_SHORT)
                    .show()
        }
        createAccountOptionMenuHandler = {
            Toast.makeText(this@MultipleUserSignInScreenActivity, "Switch Or Add User button clicked.", Toast.LENGTH_SHORT).show()
        }
        signInScreen.querySearchTextListener = object : androidx.appcompat.widget.SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                Toast.makeText(this@MultipleUserSignInScreenActivity, query
                        ?: "", Toast.LENGTH_SHORT).show()
                return true
            }

            override fun onQueryTextChange(newText: String?) = true
        }
    }
    setSupportActionBar(multiple_user_app_bar)
    supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_multiple_user_sign_in);
    SignInWithActionBarScreenSettings settings = new SignInWithActionBarScreenSettings();
    settings.setShowSwitchButton(sharedPreferences
            .getBoolean(KEY_MULTIPLE_USER_SHOW_SWITCH_BUTTON, true));
    settings.setShowForgetPasscodeButton(sharedPreferences
            .getBoolean(KEY_MULTIPLE_USER_SHOW_FORGOT_BUTTON, true));
    settings.setScreenTitle(sharedPreferences.getString(
            KEY_MULTIPLE_USER_SIGN_IN_TITLE,
            getString(R.string.basic_auth_title)
            )
    );
    settings.setSigninButtonLabel(sharedPreferences.getString(
            KEY_MULTIPLE_USER_SIGN_IN_BUTTON_LABEL,
            getString(R.string.passcode_enter_button_text)
    ));
    SignInWithActionBarScreen signInScreen = findViewById(R.id.signInScreen);
    {
        signInScreen.initialize(settings);
        signInScreen.setUserName("Test User", "test_user@sap.com");
        signInScreen.showError(5);
        signInScreen.setForgetPasscodeClickListener(view -> {
            signInScreen.clearError();
            Toast.makeText(this, "Forgot Passcode? button clicked.", Toast.LENGTH_SHORT).show();
            return null;
        });
        signInScreen.setSwitchAccountClickListener(view -> {
            switchToUserView(signInScreen, true);
            List<String> users = new ArrayList<>();
            CustomAdapter adapter = new CustomAdapter(users);
            signInScreen.getUserListView().setLayoutManager(new LinearLayoutManager(this));
            signInScreen.getUserListView().setAdapter(adapter);
            return null;
        });
        signInScreen.setSignInClickListener(view -> {
            Toast.makeText(this,
                    "Sign In button clicked.", Toast.LENGTH_SHORT
            ).show();
            return null;
        });
        signInScreen.setCreateAccountOptionMenuHandler( () -> {
            Toast.makeText(this, "Switch Or Add User button clicked.", Toast.LENGTH_SHORT).show();
            return null;
        });
        signInScreen.setQuerySearchTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return true;
            }
        });
    }
    setSupportActionBar(findViewById(R.id.multiple_user_app_bar));
    if(getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

You can hide the Switch Account button at the bottom of the screen to use it in a single-user mode mobile app.

When using this screen in a multiple-user mode mobile app, besides displaying the Switch Account button at the bottom, the client code should also provide an action bar defined in the layout file of the activity hosting this screen. The action bar will be used as the container of the action buttons.

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/multiple_user_app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/multiple_user_app_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/onboarding_background"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextColor="#000000" />
    ...

Last update: May 28, 2021