Code:-
XML Code:-
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Registration"
android:id="@+id/txtV1"
android:textSize="26sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="10dp"
/>
<EditText
android:id="@+id/userid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtV1"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:hint="Enter user ID"
android:minHeight="48dp" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/userid"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:hint="Enter username"
android:minHeight="48dp" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/username"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:hint="Enter password"
android:minHeight="48dp" />
<EditText
android:id="@+id/repassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/password"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:hint="Enter repassword"
android:minHeight="48dp" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/repassword"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:hint="Enter your Name"
android:minHeight="48dp" />
<EditText
android:id="@+id/city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:hint="Enter your city"
android:minHeight="48dp" />
<EditText
android:id="@+id/designation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/city"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:hint="Enter your designatioin"
android:minHeight="48dp" />
<Button
android:id="@+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/designation"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:text="Register" />
<Button
android:id="@+id/btn_sgin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Existing users! go to sign in page"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_below="@+id/btn_signup" />
</RelativeLayout>
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".LoginActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/txtV1"
android:textSize="26sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="50dp"
/>
<EditText
android:id="@+id/username1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtV1"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_marginRight="20dp"
android:hint="Enter username"
android:minHeight="48dp" />
<EditText
android:id="@+id/password1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/username1"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_marginRight="20dp"
android:hint="Enter username"
android:minHeight="48dp" />
<Button
android:id="@+id/btnlogin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/password1"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_marginRight="20dp"
android:text="Register" />
</RelativeLayout>
activity_home.xml
<?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=".HomeActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
Java Code:-
package com.example.mansamcapro1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText userid,username,password,repassword,name,city,designation;
Button signin,signup;
DBHelper DB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userid = (EditText)findViewById(R.id.userid);
username =(EditText)findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
repassword = (EditText)findViewById(R.id.repassword);
name = (EditText) findViewById(R.id.name);
city = (EditText)findViewById(R.id.city);
designation = (EditText)findViewById(R.id.designation);
signup = (Button) findViewById(R.id.btn_signup);
signin = (Button) findViewById(R.id.btn_sgin);
DB = new DBHelper(this);
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userID = userid.getText().toString();
String userName = username.getText().toString();
String passWord = password.getText().toString();
String repass = repassword.getText().toString();
String Name = name.getText().toString();
String City = city.getText().toString();
String Designation = designation.getText().toString();
if(userID.equals("") || userName.equals("") || passWord.equals("") || repass.equals("") || Name.equals("") || City.equals("") || Designation.equals("")){
Toast.makeText(MainActivity.this,"Please Enter All Field",Toast.LENGTH_SHORT).show();
}
else{
if (passWord.equals(repass)) {
Boolean checkuser = DB.checkusername(userName);
if (checkuser == false) {
Boolean insert = DB.inserData(userID,userName,passWord,Name,City,Designation);
if (insert == true) {
Toast.makeText(MainActivity.this,"Registered Successfully",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this,"Registration failed",Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(MainActivity.this,"User Already Exists. Please Sign In",Toast.LENGTH_SHORT).show();
}
}else
{
Toast.makeText(MainActivity.this,"Password Not Matching ",Toast.LENGTH_SHORT).show();
}
}
}
});
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
startActivity(intent);
}
});
}
}
LoginActivity.java
package com.example.mansamcapro1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
EditText userName1, passWord1;
Button Login1;
DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userName1 = (EditText) findViewById(R.id.username1);
passWord1 = (EditText) findViewById(R.id.password1);
Login1 = (Button)findViewById(R.id.btnlogin1);
db = new DBHelper(this);
Login1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = userName1.getText().toString();
String pass = passWord1.getText().toString();
if (user.equals("") || pass.equals("")) {
Toast.makeText(LoginActivity.this,"Please Enter all the fields",Toast.LENGTH_SHORT).show();
}else{
Boolean checkuserpass = db.checkusernamepass(user,pass);
if (checkuserpass == true) {
Toast.makeText(LoginActivity.this,"Sign in successfully",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this,"Login fail,Invalid crediential",Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
HomeActivity.java
package com.example.mansamcapro1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}
DBHelper.java
package com.example.mansamcapro1;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "Logemployee.db";
private static final String TABLE_Users = "employee";
private static final String KEY_ID = "id";
private static final String KEY_UNAME = "user_name";
private static final String KEY_UPASS = "user_pass";
private static final String KEY_NAME = "user";
private static final String KEY_CITY = "city";
private static final String KEY_DESIG = "Designation";
public DBHelper(@Nullable Context context) {
super(context,DBNAME,null,1);
}
@Override
public void onCreate(SQLiteDatabase MyDb) {
MyDb.execSQL("create Table employee(userID Text Primary Key,userName Text,passWord Text,Name Text,City Text,Designation Text)");
}
@Override
public void onUpgrade(SQLiteDatabase MyDb,int i,int i1) {
MyDb.execSQL("drop Table if exists employee");
}
public Boolean inserData(String userID,String userName,String passWord,String name,String city,String designation) {
SQLiteDatabase MyDb = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("userID",userID);
contentValues.put("userName",userName);
contentValues.put("passWord",passWord);
contentValues.put("Name",name);
contentValues.put("City",city);
contentValues.put("Designation",designation);
long result = MyDb.insert("employee",null,contentValues);
if(result==-1) return false;
else return true;
}
public Boolean checkusername(String userName){
SQLiteDatabase MyDb = this.getWritableDatabase();
Cursor cursor = MyDb.rawQuery("select * from employee where userName=?",new String[]{userName});
if (cursor.getCount() > 0)
return true;
else
return false;
}
public Boolean checkusernamepass(String userName,String passWord){
SQLiteDatabase MyDb = this.getWritableDatabase();
Cursor cursor = MyDb.rawQuery("select * from employee where userName=? and passWord=?",new String[]{userName, passWord});
return cursor.getCount() > 0;
}
}
Output:-

No comments:
Post a Comment