Tuesday, 7 January 2014

Login Validation

You can download the source code from here

Step 1: Create a project (Follw This)

Step 2: Open your activity_main.xml and then paste the given below

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UserName" />
 
    <EditText
        android:id="@+id/edit_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"  />
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password" />
 
    <EditText
        android:id="@+id/edit_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"  />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login" />
    <Button
        android:id="@+id/button_cancel"
        android:layout_toRightOf="@id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel "/>
 </RelativeLayout>
</LinearLayout>
Now, you have created the design for your login page as like this


Step 3: Now, you want to create another page for perform the onClick event. This page act as a second page on your login project. Please follow the steps for creating secondpage.

projectName -->src--> packagename--> right-click on packagename --> select NEW --> select other



Step 4: After you click other option, you can see the second window like this and follow as it is on the window


Step 5: Once click NEXt from the first page, the following blank activity option chosed by default don't change that and click NEXT


Step 6: Type SecondPage on the ActivityName field and click finish don't refer next button




Step 7: Once you click finish your window will be open like this with .java and .xm file



Step 8: Open up the activity_second_page.xml file and copy the below code

<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=".SecondPage" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to SecondPage" />

</RelativeLayout>


Step 9: Now open up the MainActivity.java file which located at your projectname/src/package/MainActivity.java and now copy the code which is given below

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

Button login,cancel;
EditText username;
EditText password;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username=(EditText)findViewById(R.id.edit_user);
        password=(EditText)findViewById(R.id.edit_pwd);
        login=(Button)findViewById(R.id.button_login);
        cancel=(Button)findViewById(R.id.button_cancel);
        login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
String user=null;
String pwd=null;
user=username.getText().toString();
pwd=password.getText().toString();

if(user.equals(pwd))
{
Toast.makeText(MainActivity.this, "Login Success", Toast.LENGTH_LONG).show();
Intent in=new Intent(MainActivity.this,SecondActivity.class);
startActivity(in);
}
else
{
Toast.makeText(MainActivity.this, "Login failed", Toast.LENGTH_LONG).show();
}

}
});
         cancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "You pressed cancel button", 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;
    }
 
}



Step 10: Great, You done your appliation development. Now click on start button as shown in the screenshot



Step 11: once you run your project, you can see the emulator. Wait few minutes for run your application. After your application is run, type the same word in both textfields and click Login button. If you enter same words, toast will show with success word. For your reference please look at the scree below



You can download the source code from here

No comments:

Post a Comment