Create login application where you will have to validate EmailID (UserName). Till the username and password is not validated , login button should remain disabled.
--------------------------------------------------------------------------------------
Note:- First Create New Android Application(Project) And After Followed Following Step's
Step:- 1
XML: Go To Activity_main.xml File And Add Following Code....
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0F0F0"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text=" Login Form"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#176CEC"
android:textStyle="bold" />
<EditText
android:id="@+id/editText_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:ems="10"
android:hint="Enter Email Id"
android:inputType="textEmailAddress"
android:padding="12dp" />
<Button
android:id="@+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#176CEC"
android:text="Check Valid Email"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
Step:- 2
JAVA:- Go To Activity_main.java File And Write Following Code....
package com.chirag.emailvalidation;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.Menu;
public class MainActivity extends Activity {
EditText emailEditText;
Button btn_click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailEditText = (EditText) findViewById(R.id.editText_email);
btn_click=(Button)findViewById(R.id.btn_signup);
btn_click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final String email = emailEditText.getText().toString();
if (!isValidEmail(email))
{
emailEditText.setError("Invalid Email");
Toast.makeText(getApplicationContext(),
"Invalid Email", Toast.LENGTH_LONG).show();
btn_click.setEnabled(true);
}
else
{
//emailEditText.setError("Valid Email");
Toast.makeText(getApplicationContext(),
"Valid Email", Toast.LENGTH_LONG).show();
btn_click.setEnabled(false);
}
}
});
}
// validating email id
private boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step:- 3
Now Run Your Android Application(Project) and Check Your Email Is Validate Or Not.......
No comments:
Post a Comment