Create login application where you will have to validate username and passwords Till the username and password is not validated , login button should remain disabled
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="21dp"
android:layout_marginTop="49dp"
android:text="User Name"
android:textSize="18sp" />
<EditText
android:id="@+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvName"
android:layout_alignBottom="@+id/tvName"
android:layout_alignParentEnd="true"
android:layout_marginEnd="23dp"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/tvPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/tvName"
android:layout_below="@+id/etUsername"
android:layout_marginTop="32dp"
android:text="Password"
android:textSize="18sp" />
<EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvPass"
android:layout_alignBottom="@+id/tvPass"
android:layout_alignStart="@+id/etUsername"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="LOGIN"
/>
<TextView
android:id="@+id/tvLoginStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="100sp"
/>
</RelativeLayout>
MainActivity.java
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText etUsername, etPassword;
Button btnStatus;
TextView tvLoginStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
btnStatus = (Button) findViewById(R.id.button);
tvLoginStatus = (TextView) findViewById(R.id.tvLoginStatus);
etPassword.addTextChangedListener(TextChange);
etUsername.addTextChangedListener(TextChange);
check();
}
private TextWatcher TextChange=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
check();
}
};
public void check(){
if(etUsername.getText().toString().equals("tonystark") && etPassword.getText().toString().equals("loveyou3000")){
btnStatus.setEnabled(true);
}else{
btnStatus.setEnabled(false);
}
}
}
Output
Try to enter the correct credential and the button will be enabled
0 Comments
Post a Comment