Search in sources :

Example 1 with DrawableContainer

use of android.graphics.drawable.DrawableContainer in project material-components-android by material-components.

the class TextInputLayout method ensureBackgroundDrawableStateWorkaround.

private void ensureBackgroundDrawableStateWorkaround() {
    final int sdk = Build.VERSION.SDK_INT;
    if (sdk != 21 && sdk != 22) {
        // The workaround is only required on API 21-22
        return;
    }
    final Drawable bg = mEditText.getBackground();
    if (bg == null) {
        return;
    }
    if (!mHasReconstructedEditTextBackground) {
        // This is gross. There is an issue in the platform which affects container Drawables
        // where the first drawable retrieved from resources will propagate any changes
        // (like color filter) to all instances from the cache. We'll try to workaround it...
        final Drawable newBg = bg.getConstantState().newDrawable();
        if (bg instanceof DrawableContainer) {
            // If we have a Drawable container, we can try and set it's constant state via
            // reflection from the new Drawable
            mHasReconstructedEditTextBackground = DrawableUtils.setContainerConstantState((DrawableContainer) bg, newBg.getConstantState());
        }
        if (!mHasReconstructedEditTextBackground) {
            // If we reach here then we just need to set a brand new instance of the Drawable
            // as the background. This has the unfortunate side-effect of wiping out any
            // user set padding, but I'd hope that use of custom padding on an EditText
            // is limited.
            ViewCompat.setBackground(mEditText, newBg);
            mHasReconstructedEditTextBackground = true;
        }
    }
}
Also used : ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) DrawableContainer(android.graphics.drawable.DrawableContainer) Paint(android.graphics.Paint)

Example 2 with DrawableContainer

use of android.graphics.drawable.DrawableContainer in project wire-android by wireapp.

the class AddPhoneNumberPreferenceDialogFragment method ensureBackgroundDrawableStateWorkaround.

// from TextInputLayout
private void ensureBackgroundDrawableStateWorkaround(EditText editText) {
    final int sdk = Build.VERSION.SDK_INT;
    if (sdk != 21 && sdk != 22) {
        // The workaround is only required on API 21-22
        return;
    }
    final Drawable bg = editText.getBackground();
    if (bg == null) {
        return;
    }
    // There is an issue in the platform which affects container Drawables
    // where the first drawable retrieved from resources will propogate any changes
    // (like color filter) to all instances from the cache. We'll try to workaround it...
    final Drawable newBg = bg.getConstantState().newDrawable();
    boolean hasReconstructedEditTextBackground = false;
    if (bg instanceof DrawableContainer) {
        // If we have a Drawable container, we can try and set it's constant state via
        // reflection from the new Drawable
        hasReconstructedEditTextBackground = DrawableUtils.setContainerConstantState((DrawableContainer) bg, newBg.getConstantState());
    }
    if (!hasReconstructedEditTextBackground) {
        // If we reach here then we just need to set a brand new instance of the Drawable
        // as the background. This has the unfortunate side-effect of wiping out any
        // user set padding, but I'd hope that use of custom padding on an EditText
        // is limited.
        editText.setBackgroundDrawable(newBg);
    }
}
Also used : Drawable(android.graphics.drawable.Drawable) InsetDrawable(android.graphics.drawable.InsetDrawable) DrawableContainer(android.graphics.drawable.DrawableContainer) SuppressLint(android.annotation.SuppressLint)

Example 3 with DrawableContainer

use of android.graphics.drawable.DrawableContainer in project wire-android by wireapp.

the class AddPhoneNumberPreferenceDialogFragment method clearColorFilter.

// from TextInputLayout
@TargetApi(Build.VERSION_CODES.KITKAT)
private static void clearColorFilter(@NonNull Drawable drawable) {
    drawable.clearColorFilter();
    if (Build.VERSION.SDK_INT == 21 || Build.VERSION.SDK_INT == 22) {
        // children manually
        if (drawable instanceof InsetDrawable) {
            clearColorFilter(((InsetDrawable) drawable).getDrawable());
        } else if (drawable instanceof DrawableWrapper) {
            clearColorFilter(((DrawableWrapper) drawable).getWrappedDrawable());
        } else if (drawable instanceof DrawableContainer) {
            final DrawableContainer container = (DrawableContainer) drawable;
            final DrawableContainer.DrawableContainerState state = (DrawableContainer.DrawableContainerState) container.getConstantState();
            if (state != null) {
                for (int i = 0, count = state.getChildCount(); i < count; i++) {
                    clearColorFilter(state.getChild(i));
                }
            }
        }
    }
}
Also used : DrawableWrapper(android.support.v4.graphics.drawable.DrawableWrapper) InsetDrawable(android.graphics.drawable.InsetDrawable) DrawableContainer(android.graphics.drawable.DrawableContainer) SuppressLint(android.annotation.SuppressLint) TargetApi(android.annotation.TargetApi)

Aggregations

DrawableContainer (android.graphics.drawable.DrawableContainer)3 SuppressLint (android.annotation.SuppressLint)2 Drawable (android.graphics.drawable.Drawable)2 InsetDrawable (android.graphics.drawable.InsetDrawable)2 TargetApi (android.annotation.TargetApi)1 Paint (android.graphics.Paint)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 DrawableWrapper (android.support.v4.graphics.drawable.DrawableWrapper)1