Android Open Source - built.io-todo-android Custom Edit Text Error Field






From Project

Back to project page built.io-todo-android.

License

The source code is released under:

Terms of Use & Licensing<br />Hand Drawn Web Icon Set is free and it will always be free. You can use it for both commercial and non-commercial projects. You can modify the icons anyway you like. Howe...

If you think the Android project built.io-todo-android 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.raweng.built.userInterface;
/* www .  j  av  a  2s .  co m*/
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.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.EditText;


/**
 * custom edit text.
 * 
 * @author raw engineering, Inc
 *
 */
public class CustomEditTextErrorField extends EditText {
    private final String TAG = "CustomEditTextErrorField";


  public CustomEditTextErrorField(Context context) {
        super(context);
    }

    public CustomEditTextErrorField(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditTextErrorField(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 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);
    }

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

    
    @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) {
            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 != 16 &&
                android.os.Build.VERSION.SDK_INT != 17)
            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 error) {
          error.printStackTrace();
        }
    }
}




Java Source Code List

android.UnusedStub.java
com.example.todoapp.LoginActivity.java
com.example.todoapp.TaskModel.java
com.example.todoapp.TaskViewHolder.java
com.example.todoapp.TodoActivity.java
com.example.todoapp.TodoAppApplication.java
com.raweng.built.userInterface.BuiltAuthResultCallBack.java
com.raweng.built.userInterface.BuiltDetails.java
com.raweng.built.userInterface.BuiltListViewResultCallBack.java
com.raweng.built.userInterface.BuiltTwitterLoginActivity.java
com.raweng.built.userInterface.BuiltUIListViewController.java
com.raweng.built.userInterface.BuiltUILoginController.java
com.raweng.built.userInterface.BuiltUIPickerController.java
com.raweng.built.userInterface.BuiltUISignUpController.java
com.raweng.built.userInterface.BuiltioUserInterfaceActivity.java
com.raweng.built.userInterface.CustomEditTextErrorField.java
com.raweng.built.userInterface.GetGoogleAccessToken.java
com.raweng.built.userInterface.IGoogleLoginDelegate.java
com.raweng.built.userInterface.ResultDataSource.java
com.raweng.built.userInterface.UIAndroidExplorerScreen.java
com.raweng.built.userInterface.pulltorefresh.ILoadingLayout.java
com.raweng.built.userInterface.pulltorefresh.IPullToRefresh.java
com.raweng.built.userInterface.pulltorefresh.LoadingLayoutProxy.java
com.raweng.built.userInterface.pulltorefresh.OverscrollHelper.java
com.raweng.built.userInterface.pulltorefresh.PullToRefreshAdapterViewBase.java
com.raweng.built.userInterface.pulltorefresh.PullToRefreshBase.java
com.raweng.built.userInterface.pulltorefresh.PullToRefreshListView.java
com.raweng.built.userInterface.pulltorefresh.internal.EmptyViewMethodAccessor.java
com.raweng.built.userInterface.pulltorefresh.internal.FlipLoadingLayout.java
com.raweng.built.userInterface.pulltorefresh.internal.IndicatorLayout.java
com.raweng.built.userInterface.pulltorefresh.internal.LoadingLayout.java
com.raweng.built.userInterface.pulltorefresh.internal.RotateLoadingLayout.java
com.raweng.built.userInterface.pulltorefresh.internal.Utils.java
com.raweng.built.userInterface.pulltorefresh.internal.ViewCompat.java