GTU DE Sem6 – 3360704 – 3 – Create login application where you will have to validate EmailID (UserName). Till the username and password is not validated , login button should remain disabled.

GTU DE Sem6 – 3360704 – 3 – Create login application where you will have to validate EmailID (UserName). Till the username and password is not validated , login button should remain disabled.

DE Sem6 - 3360704 - 3 - Create login application where you will have to validate EmailID

 

In this Android Application I am going to explain how we can validate email address using regular expression. Basically when user enters email address it should be in proper form, Sometimes user make mistakes while entering email address, so we can implement following code to check weather user has enter a correct email address or not. This can be done using Pattern class from java.util.regex.Matcher. and you need to use matches(). Matches() will return either true or false you can take some actions on this. Before you use matches() you need to use Pattern.Compile using exact regular expression of Email Address.

Jump to Android Program 4

FileName: MainActivity.java

/*

Mobile Computing and Application Development : 3360704

GTU DE Sem6 - 3360704 - 3 - Create login application where you will have to validate EmailID 
(UserName). Till the username and password is not validated , login button should remain disabled.

  
*/

package gtu.de.mcad.ex03;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	EditText et1,et2;
	Button b1;
	Pattern p1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		
		et1 = (EditText) findViewById(R.id.editText1);
		et2 = (EditText) findViewById(R.id.editText2);
		b1 = (Button) findViewById(R.id.button1);
		
		//Disable Button on Create
		b1.setEnabled(false);
		
		p1 = Pattern.compile(".+@.+\\.[a-z]+");
		
		et1.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
			
				String s1 = et1.getText().toString();
				Matcher m = p1.matcher(s1);
				if(m.matches())
				{
					b1.setEnabled(true);
					
				}
				else
				{
					b1.setEnabled(false);
					Toast.makeText(getApplicationContext(), "Invalid emailId", Toast.LENGTH_LONG).show();
				}
			}
		});
		
		
		
	}

	@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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		
		return super.onOptionsItemSelected(item);
	}
}

 

 FileName: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="52dp"
        android:layout_toLeftOf="@+id/editText2"
        android:text="Password"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignTop="@+id/textView2"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="54dp"
        android:text="Login" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView1"
        android:ems="10" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/textView2"
        android:layout_marginRight="18dp"
        android:layout_marginTop="161dp"
        android:text="Email"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

 

Output : Initial : Disabled Login Button

DE Sem6 - 3360704 - 3 - Create login application where you will have to validate EmailID

Output After You Enter proper Email Address

DE Sem6 - 3360704 - 3 - Create login application where you will have to validate EmailID

Leave a Reply