Tuesday 12 March 2013

ANDROID FORM VALIDATION EXAMPLE

Form Validation Example:

This is a Examle showing how to validat the data user entered
The form validation has done using the RegularExpressions 


1.Create New Android Projet


2.Add the following data in the main_activity.xml
res/layout/main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Username" />

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

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password" />

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>

3.Create The Activity Class For The xml File As Shown Below

package com.example.gurumurthy;

import java.util.regex.Pattern;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
private static final Pattern EMAIL_PATTERN = Pattern
.compile("[a-zA-Z0-9+._%-+]{1,100}" + "@"
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,10}" + "(" + "."
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,20}"+
             ")+");
private static final Pattern USERNAME_PATTERN = Pattern
.compile("[a-zA-Z0-9]{1,250}");
private static final Pattern PASSWORD_PATTERN = Pattern
.compile("[a-zA-Z0-9+_.]{4,16}");
EditText usname;
EditText pword;
EditText email1;
Button btSubmit;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("HELLO","ONCREATE");
usname = (EditText) findViewById(R.id.editText1);
pword = (EditText) findViewById(R.id.editText2);
email1 = (EditText) findViewById(R.id.editText3);
btSubmit = (Button) findViewById(R.id.btnSubmit);

btSubmit.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
String username = usname.getText().toString();
String password = pword.getText().toString();
String email = email1.getText().toString();
if (username.equals("") || password.equals("")
|| email.equals("")) {
if (username.equals("")) {
Toast.makeText(MainActivity.this, "ENTER USERNAME",
Toast.LENGTH_LONG).show();

}
if (password.equals("")) {
Toast.makeText(MainActivity.this, "ENTER PASSWORD",
Toast.LENGTH_LONG).show();

}
if (email.equals("")) {
Toast.makeText(MainActivity.this, "ENTER EMAIL ID",
Toast.LENGTH_LONG).show();

}
} else {
if (!CheckUsername(username)) {
Toast.makeText(MainActivity.this, "ENTER VALID USERNAME",
Toast.LENGTH_LONG).show();
}
if (!CheckPassword(password)) {
Toast.makeText(MainActivity.this, "ENTER VALID PASSWORD",
Toast.LENGTH_LONG).show();
}
if (!CheckEmail(email)) {
Toast.makeText(MainActivity.this, "ENTER VALID EMAIL ID",
Toast.LENGTH_LONG).show();
}
}

}

private boolean CheckEmail(String email) {

return EMAIL_PATTERN.matcher(email).matches();
}

private boolean CheckPassword(String password) {

return PASSWORD_PATTERN.matcher(password).matches();
}

private boolean CheckUsername(String username) {

return USERNAME_PATTERN.matcher(username).matches();
}
});

}

}


4.After creating an Activity file Register the Activity File in the manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gurumurthy"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gurumurthy.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


COMMENT DON'T FORGET

Wednesday 6 March 2013

ANDROID LOGIN AND REGISTRATION PAGE DESIGN

1.Cretae a new ANDROID PROJECT
      File>>New>>Android Project


2.Desining Login Sreen
res/layout/login.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/logEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/email"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <EditText
        android:id="@+id/logeditEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:inputType="textEmailAddress"
        android:hint="@string/hintemail"/>
    <TextView
        android:id="@+id/logpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pass"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <EditText
        android:id="@+id/logeditpass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/passHint"/>
    <Button
       android:id="@+id/logButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/logregister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/registernew"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

</LinearLayout>

3.Desining The Registration Sreen
res/layout/register.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/regName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <EditText
        android:id="@+id/regeditName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:inputType="text"
        android:hint="@string/hint"/>

    <TextView
        android:id="@+id/regEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/email"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <EditText
        android:id="@+id/regeditEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:inputType="textEmailAddress"
        android:hint="@string/hint"/>
    <TextView
        android:id="@+id/regpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pass"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <EditText
        android:id="@+id/regeditpass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/passHint"/>
    <Button
       android:id="@+id/regButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/register"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

4.Defining Values In The String.sml
res/values/string.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">LOGINANDREGISTER</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="name">UserName</string>
    <string name="email">Email</string>
    <string name="hint">Enter Username</string>
    <string name="hintemail">Enter Email</string>
    <string name="pass">Password</string>
    <string name="passHint">Enter Password</string>
    <string name="login">Login</string>
    <string name="registernew">New User?Register Here</string>
    <string name="register">Register</string>

</resources>

5.Creating Activity Classes For Login And Register Sreens

5.1 Activity Class For login.xml

src/LoginActivity.java


package com.example.loginandregister;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class LoginActivity extends Activity {
TextView Newreg;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.login);

Newreg = (TextView)findViewById(R.id.logregister);

Newreg.setOnClickListener(new View.OnClickListener() {

public void onClick(View v)
{
Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(intent);


}
});


}

}


5.2 Creating Activity Class For register.xml
src/RegisterActivity.xml

package com.example.loginandregister;

import android.app.Activity;
import android.os.Bundle;

public class RegisterActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}

}

6.Defing The Activity Class In Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.loginandregister"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.loginandregister.LoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name="com.example.loginandregister.RegisterActivity"
            android:label="@string/app_name" ></activity>
    </application>

</manifest>



Comment,Don't Forget