Experiment 27
Experiment 27
1.MainActivity.java
package com.example.loginapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
loginButton = findViewById(R.id.loginButton);
registerButton = findViewById(R.id.registerRedirect);
loginButton.setOnClickListener(view -> {
String user = username.getText().toString().trim();
String pass = password.getText().toString().trim();
if (dbHelper.checkUser(user, pass)) {
Toast.makeText(MainActivity.this, "Login Successful",
Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,
HomeActivity.class));
finish();
} else {
Toast.makeText(MainActivity.this, "Login Unsuccessful",
Toast.LENGTH_LONG).show();
}
});
registerButton.setOnClickListener(view -> {
startActivity(new Intent(MainActivity.this,
RegisterActivity.class));
});
}
}
—----------------------------------------------------------------------------------------------------------------------------
Add the following in your manifest file:
<activity android:name=".RegisterActivity" />
<activity android:name=".HomeActivity" />
Hence, AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.LoginApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</application>
</manifest>
—-------------------------------------------------------------------------------------------------
2.RegisterActivity.java
package com.example.loginapp;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
registerButton = findViewById(R.id.registerButton);
dbHelper = DatabaseHelper.getInstance(this);
registerButton.setOnClickListener(view -> {
String user = username.getText().toString().trim();
String pass = password.getText().toString().trim();
if (user.isEmpty() || pass.isEmpty()) {
Toast.makeText(RegisterActivity.this, "Fields cannot be empty",
Toast.LENGTH_SHORT).show();
} else {
if (dbHelper.registerUser(user, pass)) {
Toast.makeText(RegisterActivity.this, "Registration
Successful", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterActivity.this,
MainActivity.class));
finish();
} else {
Toast.makeText(RegisterActivity.this, "User already
exists", Toast.LENGTH_SHORT).show();
}
}
});
}
}
—-------------------------------------------------------------------------------------------------
3.HomeActivity.java
package com.example.loginapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_USERNAME + " TEXT UNIQUE, " +
COL_PASSWORD + " TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center"
android:background="#EEEEEE">
<EditText android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Username" />
<EditText android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword" />
<Button android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
<Button android:id="@+id/registerRedirect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
—-----------------------------------------------------------------------------
2.activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center"
android:background="#EEEEEE">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Username" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword" />
<Button
android:id="@+id/registerButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
—-----------------------------------------------------------------------------
3.activity_home.xml
<TextView
android:id="@+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome!"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#000000"/>
</LinearLayout>
—------------------------------------------------------