Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

“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

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


<RelativeLayout 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"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login Form 2"
android:fontFamily="sans-serif-black"
android:textSize="25dp"
android:id="@+id/t1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textColor="@color/black"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/username1"
android:text="Enter Username:"
android:layout_below="@+id/t1"
android:fontFamily="sans-serif-black"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:textColor="@color/black"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Username"
android:ems="10"
android:layout_toEndOf="@+id/username1"
android:layout_below="@+id/t1"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:id="@+id/username"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/password1"
android:text="Enter Password:"
android:layout_below="@+id/username"
android:fontFamily="sans-serif-black"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:textColor="@color/black"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="password"
android:ems="10"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_below="@+id/username"
android:layout_toEndOf="@+id/password1"
android:id="@+id/password"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_below="@+id/password"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="@color/black"
android:fontFamily="sans-serif-black"
android:id="@+id/btn"/>

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

Button btn;
EditText ed1,ed2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ed1=findViewById(R.id.username);
ed2=findViewById(R.id.password);

btn=findViewById(R.id.btn);

ed2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if((ed1.getText().toString().equals("abc"))&&(ed2.getText().toString().equals("abc@123"))){
btn.setEnabled(true);
}
}

@Override
public void afterTextChanged(Editable s) {

}
});

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Login Successfully",Toast.LENGTH_LONG).show();
}
});

}
}

You might also like