EX 28
EX 28
Exercise
1. Write a program to create the login form with necessary validations like length of username and
password, empty text fields, count of unsuccessful login attempts. Display the login
successful/Unsuccessful toast message.
activity_main.xml android:layout_width="250dp"
android:layout_height="40dp"
<?xml version="1.0" encoding="utf-8"?> android:layout_gravity="center"
<LinearLayout android:layout_marginTop="40dp"
xmlns:android="http://schemas.android.com/apk/r
android:background="#ffffff"
es/android"
android:hint="Enter Username" />
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
<EditText
android:layout_width="match_parent"
android:id="@+id/pwd"
android:layout_height="match_parent"
android:layout_width="250dp"
android:orientation="vertical"
android:layout_height="40dp"
tools:context=".MainActivity" >
android:layout_gravity="center"
<TextView
android:layout_marginTop="10dp"
android:id="@+id/textView"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:text="Login"
android:inputType="textPassword" />
android:layout_marginTop="80dp"
<LinearLayout
android:layout_gravity="center"
android:layout_width="match_parent"
android:textAppearance="?android:attr/textAppea android:layout_height="wrap_content"
ranceLarge" />
android:orientation="horizontal"
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_marginBottom="20dp"
android:layout_height="wrap_content"
android:gravity="center" >
android:orientation="vertical"
<Button
android:layout_marginTop="50dp"
android:id="@+id/loginbtn"
android:background="#9DA7D0">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<EditText
android:text="Login" />
android:id="@+id/uname"
<Button int count = 0;
android:id="@+id/clrbtn"
android:layout_width="wrap_content" @Override
android:layout_height="wrap_content" protected void onCreate(Bundle
savedInstanceState) {
android:text="Clear" />
super.onCreate(savedInstanceState);
</LinearLayout>
setContentView(R.layout.activity_main);
<TextView
android:id="@+id/textView1"
setContentView(R.layout.activity_main);
android:layout_width="wrap_content"
loginbtn =
android:layout_height="wrap_content"
(Button)findViewById(R.id.loginbtn);
android:textColor="#ffffff"
clrbtn = (Button)findViewById(R.id.clrbtn);
android:textSize="20dp"
uname =
android:text=" Username = Servive0 \n (EditText)findViewById(R.id.uname);
Password = Servive@0"/>
pwd = (EditText)findViewById(R.id.pwd);
</LinearLayout>
loginbtn.setOnClickListener(new
</LinearLayout> View.OnClickListener() {
public void onClick(View v) {
import android.widget.Toast;
Toast.makeText(MainActivity.this,"Username
must be of at-least 8
public class MainActivity extends characters",Toast.LENGTH_SHORT).show();
AppCompatActivity { } else {
if (getpwd.length() < 8) {
Button loginbtn, clrbtn;
EditText uname, pwd; Toast.makeText(MainActivity.this,"Password
}
}
}
}
clrbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
uname.setText("");
pwd.setText("");
}
});
}
});
}
}
Output: