0% found this document useful (0 votes)
45 views3 pages

Practical NO 28

The document outlines the creation of a login application that validates username and password inputs, keeping the login button disabled until both are valid. It includes XML layout code for the user interface and Java code for the main activity, which manages input validation. The username must be at least 3 characters and the password at least 6 characters for the login button to be enabled.
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)
45 views3 pages

Practical NO 28

The document outlines the creation of a login application that validates username and password inputs, keeping the login button disabled until both are valid. It includes XML layout code for the user interface and Java code for the main activity, which manages input validation. The username must be at least 3 characters and the password at least 6 characters for the login button to be enabled.
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.

28 : Create login application where you will have to


validate username and password till the username and password is not
validated, login button should remain disabled.

• activity_main.xml

<androidx.constraintlayout.widget.ConstraintLa />
yout
<!-- Password Input -->
xmlns:android="http://schemas.android.com/ <EditText
apk/res/android" android:id="@+id/passwordEditText"
xmlns:app="http://schemas.android.com/apk/ android:layout_width="0dp"
res-auto" android:layout_height="wrap_content"
android:layout_width="match_parent" android:layout_marginStart="16dp"
android:layout_height="match_parent"> android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
<!-- Background Image (with Crop) -->
<ImageView android:background="@android:color/white
android:id="@+id/backgroundImage" "
android:layout_width="0dp" android:hint="Password"
android:layout_height="0dp" android:imeOptions="actionDone"
android:scaleType="centerCrop" android:inputType="textPassword"
android:src="@drawable/bgimg" android:singleLine="true"

app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="
parent" app:layout_constraintStart_toStartOf="paren
app:layout_constraintStart_toStartOf="paren t"
t"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@i
/> d/usernameEditText"/>
<!-- Username Input -->
<EditText <!-- Login Button -->
android:id="@+id/usernameEditText" <Button
android:layout_width="0dp" android:id="@+id/loginButton"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_marginStart="16dp" android:layout_height="wrap_content"
android:layout_marginTop="312dp" android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:background="@android:color/black"
android:background="@android:color/white android:enabled="false"
" android:text="Login"
android:hint="Username"
android:imeOptions="actionNext" android:textColor="@android:color/white"
android:inputType="text" app:layout_constraintEnd_toEndOf="parent"
android:singleLine="true" app:layout_constraintStart_toStartOf="paren
t"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@i
app:layout_constraintStart_toStartOf="paren d/passwordEditText"/>
t"
</androidx.constraintlayout.widget.Constrain
app:layout_constraintTop_toTopOf="parent" tLayout>
• MainActivity.java

package com.example.twentyeight; EditText fields


private final TextWatcher textWatcher =
import android.os.Bundle; new TextWatcher() {
import android.text.Editable; @Override
import android.text.TextWatcher; public void
import android.widget.Button; beforeTextChanged(CharSequence s, int start,
import android.widget.EditText; int count, int after) {}

import @Override
androidx.appcompat.app.AppCompatActivity; public void
onTextChanged(CharSequence s, int start, int
public class MainActivity extends before, int count) {}
AppCompatActivity {
@Override
private EditText usernameEditText, public void afterTextChanged(Editable
passwordEditText; s) {
private Button loginButton; validateInputs();
}
@Override };
protected void onCreate(Bundle
savedInstanceState) { // Method to validate username and
super.onCreate(savedInstanceState); password
setContentView(R.layout.activity_main); private void validateInputs() {
String username =
usernameEditText = usernameEditText.getText().toString().trim();
findViewById(R.id.usernameEditText); String password =
passwordEditText = passwordEditText.getText().toString().trim();
findViewById(R.id.passwordEditText);
loginButton = // Check if username and password meet
findViewById(R.id.loginButton); the required conditions
boolean isUsernameValid =
// Add text change listeners to both username.length() >= 3; // Username must be
EditText fields at least 3 characters
boolean isPasswordValid =
usernameEditText.addTextChangedListener(t password.length() >= 6; // Password must be
extWatcher); at least 6 characters

passwordEditText.addTextChangedListener(t // Enable button only when both fields


extWatcher); are valid

// Initially disable the login button loginButton.setEnabled(isUsernameValid &&


loginButton.setEnabled(false); isPasswordValid);
} }
}
// TextWatcher to monitor changes in
Output:

You might also like