How to Create Multi User Login Apps in Android?
if you are looking for the tutorial that shows how to create a multiple user login apps in Android and redirect to different activity based on user role, then you are in the right tutorial.
In the process of solving real world problem, you may need to develop such a system which may have multiple number of user of a single system and when each user login to the system they must need to access only the certain features of an application which means they don’t need to access all the feature. In general the administrator have access to all the features and normal user may not have. So, this tutorial is all about this.
If you don’t know how to create login form in anroid, first of all, you need to read these tutorials:
Login apps in Android: HERE
Login and Registration apps in Android HERE
So, i consider that you have the basic knowledge of all these things, and required application in your system.
Steps:
[sociallocker]
1.Open Android Studio, go to file>new>new project; give application name click next, select phone and tablets;click next, select empty activity click next and finally click finish.
Now you project will open in android studio. Go to res>layout>activity_main.xml. Double click on activity_mail.xml file, select design section. And drag and drop two edittext and one button and change their properties as shown in the video. OR simply copy following code; keep in mind that you need to change the package name and application name.
Designing code:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="55dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:layout_marginTop="16dp" android:text="Username" android:textSize="25dp" app:layout_constraintBottom_toTopOf="@+id/textView8" app:layout_constraintEnd_toStartOf="@+id/txtUser" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="60dp" android:layout_marginEnd="11dp" android:layout_marginRight="11dp" android:text="Password" android:textSize="25dp" app:layout_constraintBottom_toTopOf="@+id/textView9" app:layout_constraintEnd_toStartOf="@+id/txtPass" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView7" /> <TextView android:id="@+id/textView9" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginBottom="285dp" android:layout_marginEnd="36dp" android:layout_marginLeft="16dp" android:layout_marginRight="36dp" android:layout_marginStart="16dp" android:text="User Type" android:textSize="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/spinner" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView8" /> <EditText android:id="@+id/txtUser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="34dp" android:layout_marginTop="16dp" android:ems="10" android:inputType="textPersonName" android:text="" app:layout_constraintBottom_toTopOf="@+id/txtPass" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView7" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/txtPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="33dp" android:ems="10" android:inputType="textPersonName" android:text="" app:layout_constraintBottom_toTopOf="@+id/spinner" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView8" app:layout_constraintTop_toBottomOf="@+id/txtUser" /> <Spinner android:id="@+id/spinner" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginBottom="26dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" app:layout_constraintBottom_toTopOf="@+id/btnLogin" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView9" app:layout_constraintTop_toBottomOf="@+id/txtPass" /> <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="214dp" android:layout_marginEnd="7dp" android:layout_marginRight="7dp" android:text="Login" android:textSize="25dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/spinner" /> </android.support.constraint.ConstraintLayout>
2. Now goto java>yourpackagename.com.applicationname in my case java>com.techsupportnep.multiuser>MainActivity.java
a. Inside the class declare the following variable as:
EditText _txtUser, _txtPass; Button _btnLogin; Spinner _spinner;
b. Before writing below code you have to add two activity in your application. So for thsi:
Inside the onCreate method initialize each control as:
_txtPass=(EditText)findViewById(R.id.txtPass); _txtUser=(EditText)findViewById(R.id.txtUser); _btnLogin=(Button)findViewById(R.id.btnLogin); _spinner=(Spinner)findViewById(R.id.spinner); ArrayAdapter <CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.usertype, R.layout.support_simple_spinner_dropdown_item); _spinner.setAdapter(adapter); _btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String item = _spinner.getSelectedItem().toString(); if(_txtUser.getText().toString().equals("admin")&& _txtPass.getText().toString().equals("admin")&& item.equals("admin")){ Intent intent = new Intent(MainActivity.this, admin.class); startActivity(intent); }else if(_txtUser.getText().toString().equals("admin")&& _txtPass.getText().toString().equals("admin")&& item.equals("user")){ Intent intent = new Intent(MainActivity.this, user.class); startActivity(intent); }else { Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show(); } } });
Press shift+f10 or simply click in the play button below the menu of the android studio and then select your AVD either builtin AVD , or third party AVD or your phone device and then click proceed.
[/sociallocker]
That’s it. If you have any confusion, you can watch the following video: