Professional Documents
Culture Documents
Practical No - 28
Practical No - 28
the username and password is not validated, login button should remain disabled.”
activity_main.xml
<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
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();
}
});
}
}