Commit f2c0c5d0 authored by austin.blanke's avatar austin.blanke

Working Login

parent d45cc8f1
...@@ -12,14 +12,34 @@ ...@@ -12,14 +12,34 @@
android:theme="@style/Theme.BookingAppAustinBlanke" android:theme="@style/Theme.BookingAppAustinBlanke"
tools:targetApi="31"> tools:targetApi="31">
<activity <activity
android:name=".MainActivity" android:name=".LoginActivity"
android:exported="true"> android:exported="true">
<meta-data
android:name="android.app.lib_name"
android:value="" />
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".MainActivity"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".CreateAccountActivity"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application> </application>
</manifest> </manifest>
\ No newline at end of file
package com.example.bookingapp_austinblanke;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class CreateAccountActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createaccount_activity);
}
public void createAccount(View view) {this.createAccount();}
private void createAccount()
{
DatabaseConnector connector = new DatabaseConnector(this, null);
EditText username = findViewById(R.id.Username);
EditText password = findViewById(R.id.Password);
connector.addNewUser(username.getText().toString(), password.getText().toString());
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
}
package com.example.bookingapp_austinblanke;
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 DatabaseConnector extends SQLiteOpenHelper {
private static final String DB_NAME = "Registration.db";
private static final int DB_VERSION = 1;
private static final String TABLE_USERS = "Users";
private static final String COLUMN_UID = "uid";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";
public DatabaseConnector(Context context, SQLiteDatabase.CursorFactory factory)
{
super(context, DB_NAME, factory, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_table = "CREATE TABLE "+TABLE_USERS+"("+
COLUMN_UID+" INTEGER PRIMARY KEY, " +
COLUMN_USERNAME + " TEXT, " +
COLUMN_PASSWORD + " TEXT)";
db.execSQL(CREATE_table);
}
public void addNewUser(String username, String password)
{
ContentValues values = new ContentValues();
values.put(COLUMN_USERNAME, username);
values.put(COLUMN_PASSWORD, password);
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_USERS, null, values);
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public boolean login(String username, String password) {
String query = "SELECT * FROM " + TABLE_USERS + " WHERE " + COLUMN_USERNAME + " = \"" + username +"\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
boolean result = false;
if (cursor.moveToFirst()){
if (cursor.getString(2).equals(password))
result = true;
else
result = false;
}
cursor.close();
db.close();
return result;
}
}
...@@ -15,11 +15,29 @@ public class LoginActivity extends AppCompatActivity { ...@@ -15,11 +15,29 @@ public class LoginActivity extends AppCompatActivity {
setContentView(R.layout.login_activity); setContentView(R.layout.login_activity);
} }
public void createAccount(View view) {
Intent intent = new Intent(this, CreateAccountActivity.class);
startActivity(intent);
}
public void login(View view) { public void login(View view) {
login();
}
private void login() {
DatabaseConnector connector = new DatabaseConnector(this, null);
EditText username = findViewById(R.id.Username);
EditText password = findViewById(R.id.Password);
boolean success = connector.login(username.getText().toString(), password.getText().toString());
if (success) {
Intent intent = new Intent(this, MainActivity.class); Intent intent = new Intent(this, MainActivity.class);
startActivity(intent); startActivity(intent);
} }
else
Toast.makeText(this, "incorrect username or password", Toast.LENGTH_SHORT).show();
}
} }
\ No newline at end of file
...@@ -9,5 +9,9 @@ import android.widget.Toast; ...@@ -9,5 +9,9 @@ import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
} }
\ No newline at end of file
<?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"
android:background="#F2F1E8"
tools:context=".LoginActivity">
<EditText
android:id="@+id/Password"
android:layout_width="228dp"
android:layout_height="71dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.468" />
<EditText
android:id="@+id/Username"
android:layout_width="218dp"
android:layout_height="68dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.345" />
<Button
android:id="@+id/createAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="createAccount"
android:text="Create Account"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.673" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
android:id="@+id/CreateAccount" android:id="@+id/CreateAccount"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:onClick="createAccount"
android:text="Create Account" android:text="Create Account"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
...@@ -51,8 +52,8 @@ ...@@ -51,8 +52,8 @@
android:layout_width="218dp" android:layout_width="218dp"
android:layout_height="68dp" android:layout_height="68dp"
android:ems="10" android:ems="10"
android:hint="Username"
android:inputType="textPersonName" android:inputType="textPersonName"
android:text="Username"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497" app:layout_constraintHorizontal_bias="0.497"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment