• Have Any Question?
  • contact@eitworld.com

Write an application which validate that both passwords are matched or not





In this application, we have performed the validation related to the password. In this application, we will enter the first time password and enter the confirm password. If both passwords are not matched then error message is displayed. Here is the xml file 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_marginTop=”28dp”
android:text=”Enter Password”
android:textAppearance=”?android:attr/textAppearanceLarge” />

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/pwd1″
android:layout_below=”@+id/pwd1″
android:layout_marginTop=”28dp”
android:text=”Enter Confirm Passoword”
android:textAppearance=”?android:attr/textAppearanceLarge” />

<EditText
android:id=”@+id/pwd2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView2″
android:layout_alignRight=”@+id/pwd1″
android:layout_below=”@+id/textView2″
android:ems=”10″
android:inputType=”textPassword” />

<EditText
android:id=”@+id/pwd1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView1″
android:layout_alignParentRight=”true”
android:layout_below=”@+id/textView1″
android:ems=”10″
android:inputType=”textPassword” />

<Button
android:id=”@+id/btnValidate”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/pwd2″
android:layout_below=”@+id/pwd2″
android:layout_marginTop=”56dp”
android:text=”Check Password” />

<TextView
android:id=”@+id/tvMesage”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/btnValidate”
android:layout_alignParentBottom=”true”
android:layout_marginBottom=”64dp”
android:textAppearance=”?android:attr/textAppearanceLarge” />

</RelativeLayout>

 

In this layout file we have used the relative layout; here we used two edit texts of password type and use a button to validate the passwords. In this file, first button id is pwd1, confirm password’s name is pwd2; and id for the name is btnValidate. The java file is as follows:

 

package com.example.matchpassword;

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 {
EditText password,confirmPassword;
Button validate;
TextView message;

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

password = (EditText) findViewById(R.id.pwd1);
confirmPassword = (EditText) findViewById(R.id.pwd2);
validate = (Button) findViewById(R.id.btnValidate);
message = (TextView) findViewById(R.id.tvMesage);

validate.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String p1 = password.getText().toString();
String p2 = confirmPassword.getText().toString();
if(p1.equals(p2))
{
message.setText(“Password Matched”);
}
else
{
message.setText(“Invalid password entered”);
confirmPassword.setError(“Enter the correct password”);
}
}
});
}

@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 class name is MainAcctivity which extends the Activity class as follows:

android

After that we have declared some variables as follows:

android

Here we have declared two edit text type variables and one button type variable and one variable message to show some message on the screen of type text view. After that we have to register all the controls defined in the xml file as follows:

android

Here we have registered all the controls using their ids which are defined in the xml layout file. After that we have to perform the action on button click as follows:

android

Here we have used the onClickListener() method which uses another anonyms method and inside this method we have to overload the onClick() method which takes the view type arguments. Inside this method, we have used the getText() method to get the text that are coming from the edit text and toString() method is used to convert the data into string format and assigned to the particular variables. After that we have defined p1.equals(p2) method which is used to compare the p1 string to p2. If p1 and p2 strings are same then it will return true then if block will be executed and “password matched” message is displayed on the screen otherwise else block will be executed and “password not matched” message is displayed on the screen and confirm password field get focus and display “Enter the correct password”.

The output is as follows:

android

When entered wrong password

android

when entered correct password



Real Time Web Analytics