- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
To create a simple Login and Register app. We will create 3 activities and a sqlite database helper class. - MainActivity.java
- RegisterActivity.Java
- HomeActivity.Java
- DatabaseHelper.java
The Code snippet for MainActivity.java
package com.codingtutorials.loginsqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText username, password;
TextView signupText;
Button loginButton;
DatabaseHelper databaseHelper ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = findViewById(R.id.userNameText);
password = findViewById(R.id.passwordText);
loginButton = findViewById(R.id.loginButton);
signupText = findViewById(R.id.textSignup);
databaseHelper = new DatabaseHelper(this);
signupText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = username.getText().toString().trim();
String pass = password.getText().toString().trim();
if(user.isEmpty() || pass.isEmpty())
{
Toast.makeText(MainActivity.this, "Please enter username and password", Toast.LENGTH_SHORT).show();
}
else {
String loggedInUserName = databaseHelper.checkLogin(user,pass);
if(loggedInUserName!=null)
{
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("USERNAME", loggedInUserName);
startActivity(intent);
finish();
}
else {
Toast.makeText(MainActivity.this, "Invalid Username", Toast.LENGTH_SHORT).show();
}
} }
}); }}
Code Snippet for RegisterActivity.Java
package com.codingtutorials.loginsqlite;
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.TextView;
import android.widget.Toast;
public class RegisterActivity extends AppCompatActivity {
EditText username, password;
TextView loginText;
Button signupButton;
DatabaseHelper databaseHelper ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
username = findViewById(R.id.userNameText);
password = findViewById(R.id.passwordText);
signupButton = findViewById(R.id.signupButton);
loginText = findViewById(R.id.textLogin);
databaseHelper = new DatabaseHelper(this);
loginText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
}
});
signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = username.getText().toString().trim();
String pass = password.getText().toString().trim();
if(user.isEmpty() || pass.isEmpty())
{
Toast.makeText(RegisterActivity.this, "Please enter username and password", Toast.LENGTH_SHORT).show();
}
else {
if(databaseHelper.insertData(user,pass))
{
Toast.makeText(RegisterActivity.this, "Registration Successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, HomeActivity.class);
intent.putExtra("USERNAME", user);
startActivity(intent);
}
else {
Toast.makeText(RegisterActivity.this, "Username already exists", Toast.LENGTH_SHORT).show();
} } } }); }}
Code Snippet for Home Activity .Java
package com.codingtutorials.loginsqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class HomeActivity extends AppCompatActivity {
TextView userName;
DatabaseHelper databaseHelper;
Button logOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
userName = findViewById(R.id.textUserName);
databaseHelper = new DatabaseHelper(this);
logOut = findViewById(R.id.logOutButton);
String user = getIntent().getStringExtra("USERNAME");
userName.setText("Welcome "+ user);
logOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LogOut();
}
}); }
@Override
public void onBackPressed() {
LogOut();
super.onBackPressed();
}
private void LogOut() {
Intent intent = new Intent(HomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
}}
Code Snippet for DatabaseHelper.java
package com.codingtutorials.loginsqlite;
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 DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Login.db";
private static final String COL_1 = "ID";
private static final String COL_2 = "USERNAME";
private static final String COL_3 = "PASSWORD";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE Users(ID INTEGER PRIMARY KEY AUTOINCREMENT, USERNAME TEXT, PASSWORD TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS Users");
onCreate(db);
}
public boolean insertData(String username, String password)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, username);
contentValues.put(COL_3, password);
long result = db.insert("Users", null, contentValues);
return result != -1;
}
public String checkLogin(String username, String password)
{
SQLiteDatabase db = this.getWritableDatabase();
String[] columns ={COL_2};
String selection = "USERNAME=? AND PASSWORD = ?";
String[] selectionArgs = {username, password};
Cursor cursor = db.query("Users", columns, selection, selectionArgs, null, null, null);
String result = null;
if(cursor!=null && cursor.moveToFirst())
{
result = cursor.getString(cursor.getColumnIndex(COL_2));
cursor.close();
}
return result;
}}
Code Snippet for 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:padding="10dp"
android:background="#083c5d"
tools:context=".MainActivity">
<EditText
android:id="@+id/userNameText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:textSize="24sp"
android:layout_above="@id/passwordText"
android:padding="5dp"
android:background="#ffbb39"
android:layout_marginBottom="16dp"/>
<EditText
android:id="@+id/passwordText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textSize="24sp"
android:padding="5dp"
android:layout_above="@id/loginButton"
android:background="#ffbb39"
android:layout_marginBottom="16dp"/>
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:backgroundTint="#1d2731"
android:text="Login"
android:textSize="24sp"/>
<TextView
android:id="@+id/textSignup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/loginButton"
android:textSize="24sp"
android:text="New User... Register here"
android:layout_centerHorizontal="true"
android:textColor="#ffbb39"
android:layout_marginTop="10dp"/>
</RelativeLayout>
Code Snippet for activity_register.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:padding="10dp"
android:background="#083c5d"
tools:context=".RegisterActivity">
<EditText
android:id="@+id/userNameText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:textSize="24sp"
android:layout_above="@id/passwordText"
android:padding="5dp"
android:background="#ffbb39"
android:layout_marginBottom="16dp"/>
<EditText
android:id="@+id/passwordText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textSize="24sp"
android:padding="5dp"
android:layout_above="@id/signupButton"
android:background="#ffbb39"
android:layout_marginBottom="16dp"/>
<Button
android:id="@+id/signupButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:backgroundTint="#1d2731"
android:text="Signup"
android:textSize="24sp"/>
<TextView
android:id="@+id/textLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/signupButton"
android:textSize="24sp"
android:text="Existing User... Login here"
android:layout_centerHorizontal="true"
android:textColor="#ffbb39"
android:layout_marginTop="10dp"/>
</RelativeLayout>
Code Snippet for activity_home.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=".HomeActivity"
android:background="#083c5d"
android:padding="10dp">
<TextView
android:id="@+id/textUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_centerInParent="true"
android:textColor="#ffbb39"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/logOutButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textUserName"
android:backgroundTint="#1d2731"
android:text="LogOut"
android:textSize="24sp"/>
</RelativeLayout>
https://www.youtube.com/watch?v=LMD_lQM09po
- Get link
- X
- Other Apps
Comments
Post a Comment