Android Open Source - Android-sample Form Edit Text






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;
/* www  . j  av  a2 s.  com*/
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.EditText;

import com.andreabaccega.formedittextvalidator.Validator;

/**
 * EditText Extension to be used in order to create forms in android.
 *
 * @author Andrea Baccega <me@andreabaccega.com>
 */
public class FormEditText extends EditText {
  public FormEditText(Context context) {
    super(context);

    // FIXME how should this constructor be handled
    if(!isInEditMode())
      throw new RuntimeException("Not supported");
  }

  public FormEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    if(!isInEditMode())
      editTextValidator = new DefaultEditTextValidator(this, attrs, context);
  }

  public FormEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if(!isInEditMode())
      editTextValidator = new DefaultEditTextValidator(this, attrs, context);

  }

  /**
   * Add a validator to this FormEditText. The validator will be added in the
   * queue of the current validators.
   *
   * @param theValidator
   * @throws IllegalArgumentException
   *             if the validator is null
   */
  public void addValidator(Validator theValidator) throws IllegalArgumentException {
    editTextValidator.addValidator(theValidator);
  }

  public EditTextValidator getEditTextValidator() {
    return editTextValidator;
  }

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

  /**
   * Calling *testValidity()* will cause the EditText to go through
   * customValidators and call {@link com.andreabaccega.formedittextvalidator.Validator#isValid(EditText)}
   *
   * @return true if the validity passes false otherwise.
   */
  public boolean testValidity() {
    return editTextValidator.testValidity();
  }

  private EditTextValidator editTextValidator;


  /**
   * Keep track of which icon we used last
   */
  private Drawable lastErrorIcon = null;

  /**
   * Don't send delete key so edit text doesn't capture it and close error
   */
  @Override
  public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (TextUtils.isEmpty(getText().toString())
        && keyCode == KeyEvent.KEYCODE_DEL)
      return true;
    else
      return super.onKeyPreIme(keyCode, event);
  }

  /**
   * Resolve an issue where the error icon is hidden under some cases in JB
   * due to a bug http://code.google.com/p/android/issues/detail?id=40417
   */
  @Override
  public void setError(CharSequence error, Drawable icon) {
    super.setError(error, icon);
    lastErrorIcon = icon;

    // if the error is not null, and we are in JB, force
    // the error to show
    if (error != null /* !isFocused() && */) {
      showErrorIconHax(icon);
    }
  }


  /**
   * In onFocusChanged() we also have to reshow the error icon as the Editor
   * hides it. Because Editor is a hidden class we need to cache the last used
   * icon and use that
   */
  @Override
  protected void onFocusChanged(boolean focused, int direction,
      Rect previouslyFocusedRect) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
    showErrorIconHax(lastErrorIcon);
  }

  /**
   * Use reflection to force the error icon to show. Dirty but resolves the
   * issue in 4.2
   */
  private void showErrorIconHax(Drawable icon) {
    if (icon == null)
      return;

    // only for JB 4.2 and 4.2.1
    if (android.os.Build.VERSION.SDK_INT != Build.VERSION_CODES.JELLY_BEAN
        && android.os.Build.VERSION.SDK_INT != Build.VERSION_CODES.JELLY_BEAN_MR1)
      return;

    try {
      Class<?> textview = Class.forName("android.widget.TextView");
      Field tEditor = textview.getDeclaredField("mEditor");
      tEditor.setAccessible(true);
      Class<?> editor = Class.forName("android.widget.Editor");
      Method privateShowError = editor.getDeclaredMethod("setErrorIcon",
          Drawable.class);
      privateShowError.setAccessible(true);
      privateShowError.invoke(tEditor.get(this), icon);
    } catch (Exception e) {
      // e.printStackTrace(); // oh well, we tried
    }
  }


}




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