0% found this document useful (0 votes)
9 views

Practical NO 27

The document outlines the creation of a sample Android application with a login module that checks a username and password. On successful login, it displays 'Login Successful' in a TextView, while a failed login triggers a Toast alert stating 'Login Fail'. It includes XML layout for the user interface and Java code for the login logic and user interactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Practical NO 27

The document outlines the creation of a sample Android application with a login module that checks a username and password. On successful login, it displays 'Login Successful' in a TextView, while a failed login triggers a Toast alert stating 'Login Fail'. It includes XML layout for the user interface and Java code for the login logic and user interactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical NO.27 : Create sample application with login module.

(Check Username and password) On successful login, Change Text


View “Login Successful” and on login fail, alert user using Toast
“Login Fail”.

• activity_main.xml

<RelativeLayout android:hint="Password"
xmlns:android="http://schemas.android.com/apk android:inputType="textPassword"/>
/res/android" <Button
android:id="@+id/loginButton"
xmlns:tools="http://schemas.android.com/to android:layout_width="match_parent"
ols" android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_below="@id/passwordEditTe
android:padding="16dp" xt"
tools:context=".MainActivity"> android:layout_marginTop="16dp"
<EditText android:text="Login"/>
android:id="@+id/usernameEditText" <TextView
android:layout_width="match_parent" android:id="@+id/resultTextView"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:hint="Username" android:layout_height="wrap_content"

android:inputType="textPersonName"/> android:layout_below="@id/loginButton"
<EditText android:layout_marginTop="16dp"
android:id="@+id/passwordEditText" android:text=""
android:layout_width="match_parent" android:textSize="16sp"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_gre
android:layout_below="@id/usernameEditT en_dark"
ext" android:textStyle="bold"
android:layout_marginTop="16dp" android:gravity="center"/>
</RelativeLayout>

• MainActivity.java

package com.example.twentyseven;
private EditText usernameEditText,
import android.annotation.SuppressLint; passwordEditText;
import android.app.AlertDialog; private Button loginButton;
import android.os.Bundle; private TextView resultTextView; // Added
import android.view.View; TextView to show success message
import android.widget.Button;
import android.widget.EditText; // Dummy username and password for
import android.widget.TextView; demonstration
private static final String
import DUMMY_USERNAME = "user";
androidx.appcompat.app.AppCompatActivity; private static final String
DUMMY_PASSWORD = "password";
public class MainActivity extends
AppCompatActivity { @SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle }
savedInstanceState) { });
super.onCreate(savedInstanceState); }
setContentView(R.layout.activity_main);
private void loginSuccessful() {
usernameEditText = resultTextView.setText("Login
findViewById(R.id.usernameEditText); Successful"); // Display success message in
passwordEditText = TextView
findViewById(R.id.passwordEditText);
loginButton = resultTextView.setTextColor(getResources().
findViewById(R.id.loginButton); getColor(android.R.color.holo_green_dark));
resultTextView = }
findViewById(R.id.resultTextView); //
Initialize resultTextView private void loginFailed() {
showAlertDialog("Login Failed",
loginButton.setOnClickListener(new "Incorrect username or password. Please try
View.OnClickListener() { again.");
@Override }
public void onClick(View v) {
String username = private void showAlertDialog(String title,
usernameEditText.getText().toString().trim(); String message) {
String password = AlertDialog.Builder builder = new
passwordEditText.getText().toString().trim(); AlertDialog.Builder(this);
builder.setTitle(title);
if builder.setMessage(message);
(username.equals(DUMMY_USERNAME) builder.setPositiveButton("OK", null);
&& builder.show();
password.equals(DUMMY_PASSWORD)) { }
loginSuccessful(); }
} else {
loginFailed();
}
Output:

You might also like