How to create static Login page with username and password in Android Studio ?
OutPut
Add Dependency
For Android
implementation 'com.android.support.design:28.0.0'
For Androidx
implementation'com.google.android.material:material:1.0.0'
activity_main.xml
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:background="@color/colorPrimary"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login Here"
android:textSize="40sp"
android:textColor="#fff"
android:textAllCaps="false"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:layout_margin="20dp"
android:id="@+id/linearLay1"
>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:id="@+id/ET_Username"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
style="@style/Animation.Design.BottomSheetDialog"
/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:layout_marginTop="30dp"
android:layout_margin="20dp"
android:layout_below="@id/linearLay1"
android:id="@+id/linearLay2"
>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:id="@+id/ET_Password"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:inputType="textPassword"
style="@style/Animation.Design.BottomSheetDialog"
/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:layout_marginTop="30dp"
android:layout_margin="20dp"
android:layout_below="@id/linearLay2"
android:id="@+id/linearLay3"
>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Confirm Password"
android:id="@+id/ET_Confirm_Password"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:inputType="textPassword"
style="@style/Animation.Design.BottomSheetDialog"
/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textAllCaps="false"
android:id="@+id/Btn_Login"
android:layout_below="@id/linearLay3"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:background="#94c0ff"
android:textColor="#fff"
android:textSize="20dp"
/>
</LinearLayout>
MainActivity.java
package com.example.paytmgateway;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText mUsername, mPassword, mConfirmPass;
private Button mLoginBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsername = findViewById(R.id.ET_Username);
mPassword = findViewById(R.id.ET_Password);
mConfirmPass = findViewById(R.id.ET_Confirm_Password);
mLoginBtn = findViewById(R.id.Btn_Login);
mLoginBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
userlogin();
}
});
}
private void userlogin(){
//Getting Information From User
String username = mUsername.getText().toString();
String password = mPassword.getText().toString();
String confirmpass = mConfirmPass.getText().toString();
//Validation Check
if (TextUtils.isEmpty(username)){
mUsername.setError("Enter Username");
mUsername.requestFocus();
return;
}
if (TextUtils.isEmpty(password)){
mPassword.setError("Enter Password\n Password Must be of 6 chracters");
mPassword.requestFocus();
return;
}
if (TextUtils.isEmpty(confirmpass)){
mConfirmPass.setError("Please Confirm Your Password");
mConfirmPass.requestFocus();
if (!confirmpass.matches(password)){
Toast.makeText(this, "Confirm your Password", Toast.LENGTH_SHORT).show();
}
}
else if (username.equals("Admin") && password.equals("Admin") && confirmpass.equals(password)){
Toast.makeText(this, "Wel Come to Profile Activity", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
startActivity(intent);
}
else {
Toast.makeText(this, "Check your details", Toast.LENGTH_SHORT).show();
}
}
}
0 Comments