0% found this document useful (0 votes)
7 views4 pages

Android Login Form with SQLite Integration

The document contains XML and Java code for a login/registration Android application. The XML layout defines the user interface containing edit text fields for user input of name, password, email, and phone number. The Java code implements validation of the email field and inserts user data into an SQLite database table upon clicking a register button.

Uploaded by

Waqar Hassan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Android Login Form with SQLite Integration

The document contains XML and Java code for a login/registration Android application. The XML layout defines the user interface containing edit text fields for user input of name, password, email, and phone number. The Java code implements validation of the email field and inserts user data into an SQLite database table upon clicking a register button.

Uploaded by

Waqar Hassan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

XML_CODE:

<?xml version="1.0" encoding="utf-8"?>


<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="52dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="fname"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/editText13"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="48dp"
android:ems="10"
android:hint="lname"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/editText14"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/editText14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="36dp"
android:ems="10"
android:hint="password"
android:inputType="textPassword"
app:layout_constraintBottom_toTopOf="@+id/editText15"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/editText15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="60dp"
android:ems="10"
android:hint="email"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/editText16"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<EditText
android:id="@+id/editText16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="48dp"
android:ems="10"
android:hint="phone"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="40dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="52dp"
android:text="login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</[Link]>

JAVA_CODE:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

private boolean validateEmail() {


String emailInput = [Link]().toString().trim();

if ([Link]()) {
[Link]("Field can't be empty");
return false;
} else if (!Patterns.EMAIL_ADDRESS.matcher(emailInput).matches()) {
[Link]("Please enter a valid email address");
return false;
} else {
[Link](null);
return true;
}
}

SQLiteOpenHelper openHelper;
SQLiteDatabase db;
Button button4, btnlogin;
EditText editText12, editText13, editText14, editText15, editText16;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

openHelper = new DatabaseHelper(this);


editText12 = (EditText) findViewById([Link].editText12);
editText13 = (EditText) findViewById([Link].editText13);
editText14 = (EditText) findViewById([Link].editText14);
editText15 = (EditText) findViewById([Link].editText15);
editText16 = (EditText) findViewById([Link].editText16);
// btnlogin=(Button)findViewById([Link]);
button4 = (Button) findViewById([Link].button4);
[Link](new [Link]() {
@Override
public void onClick(View v) {
db = [Link]();
String fname = [Link]().toString();
String lname = [Link]().toString();
String pass = [Link]().toString();
String email = [Link]().toString();
String phone = [Link]().toString();
insertdata(fname, lname, pass, email, phone);
[Link](getApplicationContext(), "register successfully",
Toast.LENGTH_LONG).show();
}
});
}

public void insertdata(String fname, String lname, String pass, String email,
String phone) {
ContentValues contentValues = new ContentValues();
[Link](DatabaseHelper.COL_2, fname);
[Link](DatabaseHelper.COL_3, lname);
[Link](DatabaseHelper.COL_4, pass);
[Link](DatabaseHelper.COL_5, email);
[Link](DatabaseHelper.COL_6, phone);
long id = [Link](DatabaseHelper.TABLE_NAME, null, contentValues);
}
}

package [Link];

import [Link];
import [Link];
import [Link];

public class DatabaseHelper extends SQLiteOpenHelper {


public static final String DATABASE_NAME="[Link]";
public static final String TABLE_NAME="registeration";
public static final String COL_1="ID";
public static final String COL_2="FirstName";
public static final String COL_3="LastName";
public static final String COL_4="Password";
public static final String COL_5="Email";
public static final String COL_6="Phone";

public DatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
[Link]("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY
AUTOINCREMENT,FirstName TEXT,LastName TEXT,Password TEXT,Email TEXT,Phone TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
[Link]("DROP TABLE IF EXISTS " +TABLE_NAME);
onCreate(db);
}
}

Common questions

Powered by AI

The MainActivity class uses the validateEmail() method, which first checks if the email input field is empty, setting an error if it is. Then, it uses the Patterns.EMAIL_ADDRESS.matcher(emailInput).matches() method to verify the email format. If the email doesn't match a standard email pattern, it sets an error message indicating the email is invalid. Only a valid email passes this check, ensuring it can be used for registration .

The Toast message in MainActivity serves as a quick visual and textual feedback mechanism to inform the user of a successful registration. Displayed upon completion of data insertion into the database, it notifies the user of the outcome without requiring additional interaction, helping confirm that their action was processed. This immediate feedback enhances user experience by providing confirmation of task completion in applications, preventing users from wondering about the status or needing to check manually .

The onCreate method in the MainActivity class orchestrates user registration by setting up the UI layout with setContentView(R.layout.activity_main) and initializing the SQLiteOpenHelper for database interaction. It then links user interface elements to Java variables using findViewById() for EditText and Button components. The button for registration is then set with an OnClickListener that facilitates database operations when clicked. Through these steps, the method establishes connections between UI components, backend data handling, and user actions, providing a seamless initialization process necessary for registration functionality .

Assigning textPersonName to most EditText fields streamlines the input type configuration but could lead to issues with usability and validation, especially for email and phone fields which have specific formats. An email input type should preferably use textEmailAddress, enabling specialized on-screen keyboards and validation assistance. Similarly, a phone field might use phone or number variations for better input efficiency. However, the password field correctly uses textPassword to secure input visibility. This uniform approach may simplify initial development but sacrifices contextual input optimization, potentially compromising the user experience by not leveraging specific input configurations for varied data types .

The DatabaseHelper class contains an onUpgrade() method, which handles any necessary actions for upgrading the database format. If a database version change is detected, it drops the old table using db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME) and calls onCreate() to build the new table. This mechanism allows for a seamless transition and ensures the app can maintain data integrity and functionality when the database schema evolves .

User interface elements in the XML layout are linked to the Java code in MainActivity using findViewById(). Each UI component such as EditText or Button is identified by its unique id defined in the XML file (e.g., R.id.editText12 for the first name input). This linkage allows the Java code to manipulate the UI elements by referencing the EditText and Button objects initialized in the onCreate() method .

The margin attributes in the XML layout add space between the elements and the container as well as between consecutive elements, ensuring the components are not cluttered and have a visually appealing spacing within the interface. They contribute to the overall aesthetic and usability by providing clear demarcation and separation which enhances readability for users interacting with the app elements .

The current error handling approach for email validation in MainActivity effectively blocks invalid emails but is limited by its sequential check structure. It could be improved by providing hints or suggestions as users type, such as domain suggestions after entering '@', which is a common feature in modern forms. Additionally, consider broader input feedback beyond error prompts for smoother user experience. Implementing real-time validation as the user types rather than post-entry could also enhance usability by instantly notifying users of errors, allowing immediate correction without submission attempts .

ContentValues is used to define key/value pairs that correlate column names to the values being inserted into the database. In MainActivity, it captures the user's first name, last name, password, email, and phone number, associating each value with its respective column before inserting the data into the database table using the method db.insert(). This modular approach ensures data is organized and correctly input into the database .

User registration in MainActivity involves opening a writable database via openHelper.getWritableDatabase(), retrieving user input through EditText fields, and inserting the data into the SQLite database. Once the user input—first and last name, password, email, and phone—is obtained, the insertdata() method is called, which initializes a ContentValues instance with these inputs. This instance is then passed to db.insert() to save the entry in the database. Finally, a Toast message confirms successful registration to the user .

You might also like