How to make a login application in android studio?
we will make a Material design login form.
In the previous blog post, we learned how to make a simple login form using the SQLite database.
we will create some advanced login forms with user validation.
In this blog post, I will show you how to create a Material Design login form using the SQLite database in Android Studio.
We will create some different login forms using Materia design in Android Studio.
First, we will create the main activity signup activity, login activity and last welcome activity.
You will use login or sign in by email id.
How to create a simple android login app
Fire android studio
your project name
Choose API lavel
After choosing Java Language because we will create a login app in java.
In the default main activity, we will create a home page and put 2 buttons one is login and another is signup button.
First, we will create a database file using SQLiteOpenHelper class.package com.pd.loginsqlite; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.widget.Toast; import androidx.annotation.Nullable; public class DBmain extends SQLiteOpenHelper { private static final String DB_NAME="student.db"; private static final String TABLE_NAME="course"; private static final String KEY_NAME="username"; private static final String KEY_EMAIL="email"; private static final String KEY_PASS="password"; private static final int VER=1; Context c; public DBmain(@Nullable Context context) { super(context, DB_NAME, null, VER); } @Override public void onCreate(SQLiteDatabase db) { try{ db.execSQL("create table "+TABLE_NAME+"("+KEY_NAME+" text,"+KEY_EMAIL+" text,"+KEY_PASS+" text)"); Toast.makeText(c, "table creation successfully", Toast.LENGTH_SHORT).show(); }catch (Exception e){ Log.e("DBmai","table not creation",e); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists "+TABLE_NAME+""); onCreate(db); } } package com.pd.loginsqlite; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.widget.Toast; import androidx.annotation.Nullable; public class DBmain extends SQLiteOpenHelper { private static final String DB_NAME="student.db"; private static final String TABLE_NAME="course"; private static final String KEY_NAME="username"; private static final String KEY_EMAIL="email"; private static final String KEY_PASS="password"; private static final int VER=1; Context c; public DBmain(@Nullable Context context) { super(context, DB_NAME, null, VER); } @Override public void onCreate(SQLiteDatabase db) { try{ db.execSQL("create table "+TABLE_NAME+"("+KEY_NAME+" text,"+KEY_EMAIL+" text,"+KEY_PASS+" text)"); Toast.makeText(c, "table creation successfully", Toast.LENGTH_SHORT).show(); }catch (Exception e){ Log.e("DBmai","table not creation",e); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists "+TABLE_NAME+""); onCreate(db); } }
package com.pd.loginsqlite; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void Login(View view) { Intent intent=new Intent(MainActivity.this,LoginForm.class); startActivity(intent); } public void SignUp(View view) { Intent intent=new Intent(MainActivity.this,SignUpForm.class); startActivity(intent); } }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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:background="@drawable/loginbg" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:onClick="Login" android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.445" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button2" app:layout_constraintVertical_bias="0.098" /> <Button android:onClick="SignUp" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="300dp" android:text="SignUp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.445" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
package com.pd.loginsqlite; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Patterns; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.google.android.material.textfield.TextInputEditText; import org.w3c.dom.Text; public class SignUpForm extends AppCompatActivity { TextInputEditText eusername,eemail,epassword,erepassword; Button ebutton; DBmain db; SQLiteDatabase sq; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign_up_form); eusername=(TextInputEditText)findViewById(R.id.username); eemail=(TextInputEditText)findViewById(R.id.useremail); epassword=(TextInputEditText)findViewById(R.id.password); erepassword=(TextInputEditText)findViewById(R.id.confirm_pass); ebutton=(Button)findViewById(R.id.btnregi); db=new DBmain(this); ebutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String user = eusername.getText().toString().trim(); String email = eemail.getText().toString().trim(); String pass = epassword.getText().toString().trim(); String repass = erepassword.getText().toString().trim(); //email validation if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) { eemail.setError("enter valid email"); return; } //username validation else if (eusername.getText().toString().length() <= 2) { //if username less then 2 characters then generate error eusername.setError("Please set valid username"); } //password validation else if (epassword.getText().toString().length() <= 0) { epassword.setError("Please enter password"); } //check password length else if (epassword.getText().toString().length() <= 6) { epassword.setError("Too short password\n should 6 characters or more"); } //repassword or confirm password else if (erepassword.getText().toString().length() <= 6) { erepassword.setError("Please enter confirm password"); } //if remain empty field else if (eusername.equals("") || eemail.equals("") || epassword.equals("") || erepassword.equals("")) { Toast.makeText(SignUpForm.this, "Empty field not allow", Toast.LENGTH_SHORT).show(); }else { //match password if (pass.equals(repass)){ Boolean checkuser=checkusername(user,email); if (checkuser==false){ Boolean insert=insertData(user,email,pass); if (insert==true){ Toast.makeText(SignUpForm.this,user+ "Registration successfully", Toast.LENGTH_SHORT).show(); startActivity(new Intent(getApplicationContext(),LoginForm.class)); //after click on button clear data eusername.setText(""); eemail.setText(""); epassword.setText(""); erepassword.setText(""); }else { Toast.makeText(SignUpForm.this, "Registration failed", Toast.LENGTH_SHORT).show(); epassword.setText(""); erepassword.setText(""); } }else { Toast.makeText(SignUpForm.this, "Username already exist\n click to login", Toast.LENGTH_SHORT).show(); //clear password filed epassword.setText(""); erepassword.setText(""); } }else { Toast.makeText(SignUpForm.this, "Password Not Match", Toast.LENGTH_SHORT).show(); } } } }); } private Boolean insertData(String user, String email, String pass) { sq=db.getWritableDatabase(); ContentValues cv=new ContentValues(); cv.put("username",user); cv.put("email",email); cv.put("password",pass); long rec=sq.insert("course",null,cv); if (rec==-1){ return false; }else { return true; } } private Boolean checkusername(String user, String email) { sq=db.getWritableDatabase(); Cursor cursor=sq.rawQuery("select *from course where username=? or email=?",new String[]{user,email}); if (cursor.getCount()>0){ return true; }else{ return false; } } public void Login(View view) { Intent intent=new Intent(SignUpForm.this,LoginForm.class); startActivity(intent); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:gravity="center_vertical" android:orientation="vertical" android:background="@drawable/homebg" tools:context=".SignUpForm"> <LinearLayout android:background="@drawable/bgcolor" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mrl" android:layout_marginTop="@dimen/mt" android:layout_marginRight="@dimen/mrl" android:background="@drawable/headerbg" android:gravity="center" android:padding="25dp" android:text="Registration Form" android:textColor="@color/white" android:textSize="25dp" /> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="wrap_content" app:startIconDrawable="@drawable/ic_baseline_account_circle_24" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mrl" android:layout_marginTop="@dimen/mt" android:background="@color/white" android:layout_marginRight="@dimen/mrl" android:hint="Enter username" app:endIconMode="clear_text" app:endIconTint="#F44336"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="match_parent" android:ems="25" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="wrap_content" android:background="@color/white" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mrl" android:layout_marginTop="@dimen/mt" android:layout_marginRight="@dimen/mrl" android:hint="Enter Email" app:startIconDrawable="@drawable/ic_baseline_email_24" app:endIconMode="clear_text" app:endIconTint="#F44336"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/useremail" android:layout_width="match_parent" android:layout_height="match_parent" android:ems="25" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="wrap_content" android:background="@color/white" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mrl" android:layout_marginTop="@dimen/mt" app:startIconDrawable="@drawable/ic_baseline_vpn_key_24" android:layout_marginRight="@dimen/mrl" android:hint="Enter password" app:endIconMode="clear_text" app:endIconTint="#F44336"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/password" android:layout_width="match_parent" android:inputType="textPassword" android:layout_height="match_parent" android:ems="25" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="wrap_content" android:background="@color/white" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mrl" android:layout_marginTop="@dimen/mt" app:startIconDrawable="@drawable/ic_baseline_vpn_key_24" android:layout_marginRight="@dimen/mrl" android:hint="Confirm password" app:endIconMode="clear_text" app:endIconTint="#F44336"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/confirm_pass" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="match_parent" android:ems="25" /> </com.google.android.material.textfield.TextInputLayout> <Button android:id="@+id/btnregi" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mrl" android:layout_marginTop="@dimen/mt" android:layout_marginRight="@dimen/mrl" android:background="@drawable/btncorner" android:text="Registration" /> <TextView android:onClick="Login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Already Account" android:textColor="@color/purple_700" android:textSize="20dp" android:layout_gravity="end" android:layout_marginRight="@dimen/mrl" android:layout_marginBottom="10dp"/> </LinearLayout> </LinearLayout>
package com.pd.loginsqlite; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Patterns; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.google.android.material.textfield.TextInputEditText; public class LoginForm extends AppCompatActivity { TextInputEditText eloginuser,eloginpass; Button ebutton; DBmain db; SQLiteDatabase sq; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_form); eloginuser=(TextInputEditText)findViewById(R.id.loginuser); eloginpass=(TextInputEditText)findViewById(R.id.loginpass); ebutton=(Button)findViewById(R.id.loginbtn); db=new DBmain(this); //when click on login button ebutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String loginuser=eloginuser.getText().toString().trim(); String loginpass=eloginpass.getText().toString().trim(); //check empty field if (eloginuser.equals("")||eloginpass.equals("")){ Toast.makeText(LoginForm.this, "Empty field not allow", Toast.LENGTH_SHORT).show(); } //check email id else if (!Patterns.EMAIL_ADDRESS.matcher(loginuser).matches()){ eloginuser.setError("Enter valid email id"); }else { Boolean checkusername=checkusernamepass(loginuser,loginpass); if (checkusername==true){ Toast.makeText(LoginForm.this, "Login successfully", Toast.LENGTH_SHORT).show(); //when login successfully redirect welcome activity Intent intent=new Intent(LoginForm.this,Welcome.class); startActivity(intent); eloginuser.setText(""); eloginpass.setText(""); }else { Toast.makeText(LoginForm.this, "user name and password wrong", Toast.LENGTH_SHORT).show(); } } } }); } private Boolean checkusernamepass(String loginuser, String loginpass) { sq=db.getWritableDatabase(); Cursor cursor=sq.rawQuery("select *from course where email=? and password=?",new String[]{loginuser,loginpass}); if (cursor.getCount()>0){ return true; }else{ return false; } } public void SignUp(View view) { Intent intent=new Intent(LoginForm.this,SignUpForm.class); startActivity(intent); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:background="@drawable/loginbg" android:gravity="center_vertical" android:orientation="vertical" tools:context=".LoginForm"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:layout_marginTop="@dimen/mt" android:background="@drawable/bgcolor" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mrl" android:layout_marginTop="@dimen/mt" android:layout_marginRight="@dimen/mrl" android:background="@drawable/headerbg" android:gravity="center" android:padding="20dp" android:text="Login Form" android:textColor="@color/white" android:textSize="25dp" /> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mrl" android:layout_marginTop="@dimen/mt" android:layout_marginRight="@dimen/mrl" android:background="@color/white" android:hint="Email" app:startIconDrawable="@drawable/ic_baseline_email_24"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/loginuser" android:layout_width="match_parent" android:layout_height="match_parent" android:ems="25" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mrl" android:layout_marginTop="@dimen/mt" android:layout_marginRight="@dimen/mrl" android:background="@color/white" android:hint="Password" app:endIconMode="password_toggle" app:startIconDrawable="@drawable/ic_baseline_vpn_key_24"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/loginpass" android:layout_width="match_parent" android:layout_height="match_parent" android:ems="25" /> </com.google.android.material.textfield.TextInputLayout> <Button android:id="@+id/loginbtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/mrl" android:layout_marginTop="@dimen/mt" android:layout_marginRight="@dimen/mrl" android:background="@drawable/btncorner" android:text="Login" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:layout_marginTop="@dimen/mt" android:layout_marginRight="@dimen/mrl" android:onClick="SignUp" android:layout_marginBottom="10dp" android:text="New User Sign Up" android:textColor="@color/purple_700" android:textSize="18dp" /> </LinearLayout> </LinearLayout>
package com.pd.loginsqlite; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class Welcome extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); } }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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=".Welcome"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to Course code" android:textSize="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="mrl">30dp</dimen> <dimen name="mt">5dp</dimen> </resources>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pd.loginsqlite"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.LoginSqlite"> <activity android:name=".Welcome"></activity> <activity android:name=".SignUpForm" /> <activity android:name=".LoginForm" /> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>