Android Open Source - Android-sample Validating Edit Text Preference






From Project

Back to project page Android-sample.

License

The source code is released under:

Apache License

If you think the Android project Android-sample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.andreabaccega.widget;
/*w ww . j  ava2s .  c  om*/
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

/**
 * A validating {@link EditTextPreference} validation is performed when the OK
 * or AlertDialog.BUTTON_POSITIVE button is clicked. When invalid an error
 * message is displayed and the EditTextPreference is not dismissed
 */
public class ValidatingEditTextPreference extends EditTextPreference {
  public ValidatingEditTextPreference(Context context) {
    super(context);
    // FIXME how should this constructor be handled
    throw new RuntimeException("Not supported");
  }

  public ValidatingEditTextPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    editTextValidator = new DefaultEditTextValidator(getEditText(), attrs,
        context);
  }

  public ValidatingEditTextPreference(Context context, AttributeSet attrs,
      int defStyle) {
    super(context, attrs, defStyle);
    editTextValidator = new DefaultEditTextValidator(getEditText(), attrs,
        context);
  }

  public EditTextValidator getEditTextValidator() {
    return editTextValidator;
  }

  public void setEditTextValidator(EditTextValidator editTextValidator) {
    this.editTextValidator = editTextValidator;
  }

  @Override
  protected void showDialog(Bundle state) {
    super.showDialog(state);

    // If the dialog isn't an instance of alert dialog this code is useless
    if (super.getDialog() instanceof AlertDialog) {
      final AlertDialog theDialog = (AlertDialog) super.getDialog();

      // get originalBottomPadding to know when adjust the underlying
      // layouts bottom padding (ie has room already
      // been created for an error message)
      int padding = Integer.MIN_VALUE;
      try {
        padding = ((LinearLayout) getEditText().getParent())
            .getPaddingBottom();
      } catch (Exception e) {
        // some exception thrown. Unable to do increase space for error
        // message
      }

      final int originalBottomPadding = padding;

      Button b = theDialog.getButton(AlertDialog.BUTTON_POSITIVE);

      // attach our validating on click listener
      ValidatingOnClickListener l = new ValidatingOnClickListener(
          originalBottomPadding, theDialog);
      b.setOnClickListener(l);

      // add an editor action listener for the 'done/next' buttons on a
      // soft keyboard
      getEditText().setOnEditorActionListener(l);
    }
  }

  private final class ValidatingOnClickListener implements
      View.OnClickListener, OnEditorActionListener {
    private ValidatingOnClickListener(int originalBottomPadding,
        AlertDialog theDialog) {
      this.originalBottomPadding = originalBottomPadding;
      this.theDialog = theDialog;
    }

    public void onClick(View view) {
      performValidation();
    }

    public void performValidation() {
      getEditText().setError(null);
      if (editTextValidator.testValidity()) {
        // Dismiss once everything is OK.
        theDialog.dismiss();
        ValidatingEditTextPreference.this.onClick(theDialog,
            AlertDialog.BUTTON_POSITIVE);

        // reset padding - for when dialog is used again
        if (originalBottomPadding != Integer.MIN_VALUE) {
          LinearLayout parentLayout = (LinearLayout) getEditText()
              .getParent();

          if (originalBottomPadding == parentLayout
              .getPaddingBottom()) {
            parentLayout.setPadding(parentLayout.getPaddingLeft(),
                parentLayout.getPaddingTop(),
                parentLayout.getPaddingRight(),
                originalBottomPadding);
          }
        }
      } else {

        // increase padding so error message doesn't cover buttons
        if (originalBottomPadding != Integer.MIN_VALUE) {
          LinearLayout parentLayout = (LinearLayout) getEditText()
              .getParent();

          if (originalBottomPadding == parentLayout
              .getPaddingBottom()) {
            parentLayout
                .setPadding(
                    parentLayout.getPaddingLeft(),
                    parentLayout.getPaddingTop(),
                    parentLayout.getPaddingRight(),
                    (int) (parentLayout.getPaddingBottom() + getEditText()
                        .getHeight() * 1.05));
          }
        }

        // don't dismiss the dialog
      }
    }

    private final int originalBottomPadding;

    private final AlertDialog theDialog;

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      performValidation();
      return true;
    }

  }

  private EditTextValidator editTextValidator;

}




Java Source Code List

com.andreabaccega.formedittextvalidator.AlphaNumericValidator.java
com.andreabaccega.formedittextvalidator.AlphaValidator.java
com.andreabaccega.formedittextvalidator.AndValidator.java
com.andreabaccega.formedittextvalidator.CreditCardValidator.java
com.andreabaccega.formedittextvalidator.DateValidator.java
com.andreabaccega.formedittextvalidator.DomainValidator.java
com.andreabaccega.formedittextvalidator.DummyValidator.java
com.andreabaccega.formedittextvalidator.EmailValidator.java
com.andreabaccega.formedittextvalidator.EmptyValidator.java
com.andreabaccega.formedittextvalidator.IpAddressValidator.java
com.andreabaccega.formedittextvalidator.MultiValidator.java
com.andreabaccega.formedittextvalidator.NotValidator.java
com.andreabaccega.formedittextvalidator.NumericValidator.java
com.andreabaccega.formedittextvalidator.OrValidator.java
com.andreabaccega.formedittextvalidator.PatternValidator.java
com.andreabaccega.formedittextvalidator.PersonFullNameValidator.java
com.andreabaccega.formedittextvalidator.PersonNameValidator.java
com.andreabaccega.formedittextvalidator.PhoneValidator.java
com.andreabaccega.formedittextvalidator.RegexpValidator.java
com.andreabaccega.formedittextvalidator.SameValueValidator.java
com.andreabaccega.formedittextvalidator.Validator.java
com.andreabaccega.formedittextvalidator.WebUrlValidator.java
com.andreabaccega.widget.DefaultEditTextValidator.java
com.andreabaccega.widget.EditTextValidator.java
com.andreabaccega.widget.FormEditText.java
com.andreabaccega.widget.ValidatingEditTextPreference.java
uk.co.senab.photoview.Compat.java
uk.co.senab.photoview.IPhotoView.java
uk.co.senab.photoview.PhotoViewAttacher.java
uk.co.senab.photoview.PhotoView.java
uk.co.senab.photoview.gestures.CupcakeGestureDetector.java
uk.co.senab.photoview.gestures.EclairGestureDetector.java
uk.co.senab.photoview.gestures.FroyoGestureDetector.java
uk.co.senab.photoview.gestures.GestureDetector.java
uk.co.senab.photoview.gestures.OnGestureListener.java
uk.co.senab.photoview.gestures.VersionedGestureDetector.java
uk.co.senab.photoview.log.LogManager.java
uk.co.senab.photoview.log.LoggerDefault.java
uk.co.senab.photoview.log.Logger.java
uk.co.senab.photoview.scrollerproxy.GingerScroller.java
uk.co.senab.photoview.scrollerproxy.IcsScroller.java
uk.co.senab.photoview.scrollerproxy.PreGingerScroller.java
uk.co.senab.photoview.scrollerproxy.ScrollerProxy.java