Thursday, 23 May 2013

Show Case View Example in Android

activity_main.xml

<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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="This is Original View" />

</RelativeLayout>

overlay_view.xml

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

    <ImageView
        android:id="@+id/over_lay_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#50220000" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="This is Overlay View" />

</RelativeLayout>

MainActivity.java

package com.rajeshvijayakumar.overlay;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private ImageView mOverLayImage;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        final Dialog overlayInfo = new Dialog(MainActivity.this);
        overlayInfo.requestWindowFeature(Window.FEATURE_NO_TITLE);
        overlayInfo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        overlayInfo.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        overlayInfo.setContentView(R.layout.overlay_view);
        overlayInfo.show();
       
        mOverLayImage = (ImageView) overlayInfo.findViewById(R.id.over_lay_image);
        mOverLayImage.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {               
                overlayInfo.cancel();
            }
        });       
    }
}

Output :




Source Code: Download this Example Here




Wednesday, 22 May 2013

Confirm Password Validation using Saripaar Example in Android

Download Saripaar Library from gitHub 


Add Saripaar Library Project to your project, (same method of adding actionbarsherlock to your project)

activity_main.xml

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

    <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="18dp"
        android:layout_marginTop="42dp"
        android:text="Old Password"
        android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/old_passwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/textView1"
            android:layout_toRightOf="@+id/textView1"
            android:ems="10"
            android:inputType="textPassword" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/old_passwd"
            android:layout_marginTop="44dp"
            android:text="New Password"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/new_passwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView2"
            android:layout_alignBottom="@+id/textView2"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/textView2"
            android:ems="10"
            android:inputType="textPassword" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView2"
            android:layout_below="@+id/new_passwd"
            android:layout_marginTop="42dp"
            android:text="Confirm"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/confirm_passwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/textView3"
            android:ems="10"
            android:inputType="textPassword" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/change_password_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView3"
            android:layout_below="@+id/confirm_passwd"
            android:layout_marginLeft="39dp"
            android:layout_marginTop="55dp"
            android:text="Change Password" />

</RelativeLayout>

ConfirmPasswordActivity.java



package com.rajeshvijayakumar.confirmpassword;

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

import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.Validator.ValidationListener;
import com.mobsandgeeks.saripaar.annotation.ConfirmPassword;
import com.mobsandgeeks.saripaar.annotation.Password;
import com.mobsandgeeks.saripaar.annotation.Required;
import com.mobsandgeeks.saripaar.annotation.TextRule;

public class ConfirmPasswordActivity extends Activity implements
        ValidationListener {

    @Required(order = 1)
    @TextRule(order = 2, minLength = 6, message = "Enter a New Password Correctly")
    private EditText mOldPasswordEditText;

    @Required(order = 3)
    @Password(order = 4, message = "Enter a Valid Password")
    @TextRule(order = 5, minLength = 6, message = "Enter a New Password Correctly")
    private EditText mNewPasswordEditText;

    @Required(order = 6)
    @ConfirmPassword(order = 7, message = "Repeat a New Password Correctly")
    private EditText mConfirmNewPasswordEditText;

    private Button mChangePasswordButton;
    private Validator mChangePasswordValidator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mOldPasswordEditText = (EditText) findViewById(R.id.old_passwd);
        mNewPasswordEditText = (EditText) findViewById(R.id.new_passwd);
        mConfirmNewPasswordEditText = (EditText) findViewById(R.id.confirm_passwd);

        mChangePasswordButton = (Button) findViewById(R.id.change_password_button);
        mChangePasswordButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mChangePasswordValidator.validate();
            }
        });

        mChangePasswordValidator = new Validator(this);
        mChangePasswordValidator.setValidationListener(this);

    }

    @Override
    public void onFailure(View failedView, Rule<?> failedRule) {

        String message = failedRule.getFailureMessage();
        if (failedView instanceof EditText) {
            failedView.requestFocus();
            ((EditText) failedView).setError(message);
        } else {
            Toast.makeText(this, "Password Not Changed", Toast.LENGTH_SHORT)
                    .show();
        }
    }

    @Override
    public void onSuccess() {
        Toast.makeText(this, "SuccessFully Password Changed",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onValidationCancelled() {
        // TODO Auto-generated method stub

    }

    @Override
    public void preValidation() {
        // TODO Auto-generated method stub

    }
}


Output :

 




Source Code : Download this Example Here