• Have Any Question?
  • contact@eitworld.com

write an application for push notification and title and message should be entered by the user.





In this application, we have checked that input email is valid or not, if email is valid then the email is displayed on the screen otherwise a message “Invalid email” is shown on the screen. The xml file is as follows:

 

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” ><TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true”
android:layout_marginLeft=”98dp”
android:layout_marginTop=”55dp”
android:text=”Enter Email ID” />

<EditText
android:id=”@+id/editText1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textView1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”38dp”
android:ems=”10″ >

<requestFocus />
</EditText>

<Button
android:id=”@+id/btnValidate”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/editText1″
android:layout_below=”@+id/editText1″
android:layout_marginTop=”57dp”
android:text=”Check Email” />

<TextView
android:id=”@+id/tvMessage”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/btnValidate”
android:layout_below=”@+id/btnValidate”
android:layout_marginTop=”62dp”
android:textAppearance=”?android:attr/textAppearanceLarge” />

</RelativeLayout>

In this layout file we have used the relative layout and used a button, text view and edit text to take the input from the user. Here we have used the <requestFocus> for focusing the EditText. The java file is as follow:

package com.example.emailvalidation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
Button validate;
EditText email;
TextView message;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

validate = (Button) findViewById(R.id.btnValidate);
email = (EditText) findViewById(R.id.editText1);
message = (TextView) findViewById(R.id.tvMessage);

validate.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String input = email.getText().toString();
if(!validEmail(input))
{
email.setError(“Enter valid email”);
message.setText(“Entered email is invalid”);
}
else
{
message.setText(“Valid Email : “+input);
}
}
});
}

public boolean validEmail(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;
}
}

In this java file we have used the following packages:

android Here we have used the java.util.regex.Matcher package. Here regex means the regular expression and the Matcher class provide the matching methods or validating methods to validate the email. And after that we have used the java.util.regex.Pattern; here Pattern class provides the method to check the pattern of the email because the email has the fixed pattern. Here class MainActivity extends the Activity class as follows:

android After that we have declared the following variables:

android Here we have declared the variable validate of type Button, variable email of type EditText and variable of type TextView. Now we have to register all the controls inside the onCreate() method as follows:

android Here we have registered all the controls by using there id which is defined in the xml layout file. Now we have to perform the action on the submit button. Here we are using the setOnClickListener() method to perform action. It will work when we click on the button to check the email. Here inside this method, another method onClick() is used and inside this method we have declared a string type variable which holds the data which are coming from the EditText means when we will enter the email id in the edit text on the screen then this is hold by the string variable, after that we have used the if condition and pass a method of Boolean type that takes the string type argument. In this case, if this condition fails then if part will be executed otherwise else part will be executed; the method is as follows:

android Inside this method, we have declared a string type variable email_pattern which is initialized by a value. This variable holds the all type of combination where we use in the email making. After that we have created a Pattern class object. Here we used the static method compile to compile the email_pattern string which is used to match the input string and it is assigned to the pattern variable of type Pattern. Now we have created a pattern for the email validation. After that we have created the Matcher class object, and here we are using the matcher() method which takes one argument of type string, here we have passed the input email and this will match the input string to the pattern string and the result is returned by the matches() method. This method is of Boolean type so this method will return true or false. When control will transfer to the calling place then which condition match then result will be displayed on the screen?

The output is as follows:

android When invalid email is entered

android when valid email is entered



Real Time Web Analytics