Example usage for android.widget TextView getCurrentTextColor

List of usage examples for android.widget TextView getCurrentTextColor

Introduction

In this page you can find the example usage for android.widget TextView getCurrentTextColor.

Prototype

@ColorInt
public final int getCurrentTextColor() 

Source Link

Document

Return the current color selected for normal text.

Usage

From source file:Main.java

public static void traverseAndRecolor(View root, int color, boolean withStates,
        boolean setClickableItemBackgrounds) {
    Context context = root.getContext();

    if (setClickableItemBackgrounds && root.isClickable()) {
        StateListDrawable selectableItemBackground = new StateListDrawable();
        selectableItemBackground.addState(new int[] { android.R.attr.state_pressed },
                new ColorDrawable((color & 0xffffff) | 0x33000000));
        selectableItemBackground.addState(new int[] { android.R.attr.state_focused },
                new ColorDrawable((color & 0xffffff) | 0x44000000));
        selectableItemBackground.addState(new int[] {}, null);
        root.setBackground(selectableItemBackground);
    }// w w  w.  j  av a 2s .co m

    if (root instanceof ViewGroup) {
        ViewGroup parent = (ViewGroup) root;
        for (int i = 0; i < parent.getChildCount(); i++) {
            traverseAndRecolor(parent.getChildAt(i), color, withStates, setClickableItemBackgrounds);
        }

    } else if (root instanceof ImageView) {
        ImageView imageView = (ImageView) root;
        Drawable sourceDrawable = imageView.getDrawable();
        if (withStates && sourceDrawable != null && sourceDrawable instanceof BitmapDrawable) {
            imageView.setImageDrawable(
                    makeRecoloredDrawable(context, (BitmapDrawable) sourceDrawable, color, true));
        } else {
            imageView.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
        }

    } else if (root instanceof TextView) {
        TextView textView = (TextView) root;
        if (withStates) {
            int sourceColor = textView.getCurrentTextColor();
            ColorStateList colorStateList = new ColorStateList(
                    new int[][] { new int[] { android.R.attr.state_pressed },
                            new int[] { android.R.attr.state_focused }, new int[] {} },
                    new int[] { sourceColor, sourceColor, color });
            textView.setTextColor(colorStateList);
        } else {
            textView.setTextColor(color);
        }

    } else if (root instanceof AnalogClock) {
        AnalogClock analogClock = (AnalogClock) root;
        try {
            Field hourHandField = AnalogClock.class.getDeclaredField("mHourHand");
            hourHandField.setAccessible(true);
            Field minuteHandField = AnalogClock.class.getDeclaredField("mMinuteHand");
            minuteHandField.setAccessible(true);
            Field dialField = AnalogClock.class.getDeclaredField("mDial");
            dialField.setAccessible(true);
            BitmapDrawable hourHand = (BitmapDrawable) hourHandField.get(analogClock);
            if (hourHand != null) {
                Drawable d = makeRecoloredDrawable(context, hourHand, color, withStates);
                d.setCallback(analogClock);
                hourHandField.set(analogClock, d);
            }
            BitmapDrawable minuteHand = (BitmapDrawable) minuteHandField.get(analogClock);
            if (minuteHand != null) {
                Drawable d = makeRecoloredDrawable(context, minuteHand, color, withStates);
                d.setCallback(analogClock);
                minuteHandField.set(analogClock, d);
            }
            BitmapDrawable dial = (BitmapDrawable) dialField.get(analogClock);
            if (dial != null) {
                Drawable d = makeRecoloredDrawable(context, dial, color, withStates);
                d.setCallback(analogClock);
                dialField.set(analogClock, d);
            }
        } catch (NoSuchFieldException ignored) {
        } catch (IllegalAccessException ignored) {
        } catch (ClassCastException ignored) {
        } // TODO: catch all exceptions?
    }
}

From source file:android.support.design.testutils.TestUtilsMatchers.java

/**
 * Returns a matcher that matches TextViews with the specified text color.
 *///w  ww .j a  v a  2s . c  om
public static Matcher withTextColor(final @ColorInt int textColor) {
    return new BoundedMatcher<View, TextView>(TextView.class) {
        private String failedCheckDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText(failedCheckDescription);
        }

        @Override
        public boolean matchesSafely(final TextView view) {
            final @ColorInt int ourTextColor = view.getCurrentTextColor();
            if (ourTextColor != textColor) {
                int ourAlpha = Color.alpha(ourTextColor);
                int ourRed = Color.red(ourTextColor);
                int ourGreen = Color.green(ourTextColor);
                int ourBlue = Color.blue(ourTextColor);

                int expectedAlpha = Color.alpha(textColor);
                int expectedRed = Color.red(textColor);
                int expectedGreen = Color.green(textColor);
                int expectedBlue = Color.blue(textColor);

                failedCheckDescription = "expected color to be [" + expectedAlpha + "," + expectedRed + ","
                        + expectedGreen + "," + expectedBlue + "] but found [" + ourAlpha + "," + ourRed + ","
                        + ourGreen + "," + ourBlue + "]";
                return false;
            }
            return true;
        }
    };
}

From source file:com.metinkale.prayerapp.vakit.WidgetService.java

private static boolean recurseGroup(ViewGroup gp) {
    int count = gp.getChildCount();
    for (int i = 0; i < count; ++i) {
        View v = gp.getChildAt(i);
        if (v instanceof TextView) {
            TextView text = (TextView) v;
            String szText = text.getText().toString();
            if (COLOR_SEARCH_1ST.equals(szText)) {
                COLOR_1ST = text.getCurrentTextColor();
            }/*w  ww.ja  va2s. co  m*/
            if (COLOR_SEARCH_2ND.equals(szText)) {
                COLOR_2ND = text.getCurrentTextColor();
            }

            if ((COLOR_1ST != null) && (COLOR_2ND != null)) {
                return true;
            }
        } else if (gp.getChildAt(i) instanceof ViewGroup) {
            if (recurseGroup((ViewGroup) v)) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.hippo.android.animator.AnimatorsBase.java

static Animator recolorText(TextView view, int color) {
    return recolorText(view, view.getCurrentTextColor(), color);
}

From source file:io.github.guaidaodl.espresso.CustomViewMatchers.java

/**
 * Returns a matcher that matches the descendant of {@link TextView} that is displaying
 * text with the color associated with given color resource id.
 *
 * @param colorRes the color resource which the text view's text should be.
 *//*  ww  w .  ja  v a 2 s .  c o m*/
@NonNull
public static Matcher<View> withTextColor(@ColorRes final int colorRes) {
    return new BoundedMatcher<View, TextView>(TextView.class) {
        private Integer expectedColor = -1;
        private String expectedColorName = null;

        @Override
        public void describeTo(Description description) {
            description.appendText("with color from resource id : ").appendValue(colorRes);

            if (expectedColorName != null) {
                description.appendText("[").appendText(expectedColorName).appendText("]");
            }

            if (expectedColor != -1) {
                description.appendValue(" value: ").appendText(ColorFormat.toHexFormat(expectedColor));

            }
        }

        @Override
        protected boolean matchesSafely(TextView textView) {
            if (expectedColor == -1) {
                try {
                    //noinspection deprecation
                    expectedColor = textView.getResources().getColor(colorRes);
                    expectedColorName = textView.getResources().getResourceEntryName(colorRes);
                } catch (Resources.NotFoundException ignore) {
                }
            }

            int actualColor = textView.getCurrentTextColor();

            return expectedColor != null && expectedColor == actualColor;
        }
    };
}

From source file:com.musenkishi.atelier.Atelier.java

private static void applyColorToView(final TextView textView, int color, boolean fromCache) {
    if (fromCache) {
        textView.setTextColor(color);//from ww  w .  jav a2s  . c  om
    } else {
        Integer colorFrom = textView.getCurrentTextColor();
        Integer colorTo = color;
        ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
        colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                textView.setTextColor((Integer) animator.getAnimatedValue());
            }
        });
        colorAnimation.start();
    }
}

From source file:com.alainesp.fan.sanderson.MainActivity.java

/**
 * Set the badge number to all menu items visible
 *///from w  w  w. j  ava2  s. c o m
private static void setBadgesNumberUI() {
    if (navigationView != null) {
        NavigationMenuView v = (NavigationMenuView) navigationView.getChildAt(0);
        Menu drawerMenu = navigationView.getMenu();

        if (v != null)
            // Iterate all children
            for (int childIndex = 0; childIndex < v.getChildCount(); childIndex++) {
                View v1 = v.getChildAt(childIndex);
                if (v1 instanceof NavigationMenuItemView) {
                    TextView mTextView = (TextView) ((NavigationMenuItemView) v1).getChildAt(0);
                    if (mTextView != null) {
                        // Get the menu index
                        Integer menuIndex = reverseMenuText.get(mTextView.getText().toString());
                        if (menuIndex != null && menuIndex < badgeNumbers.length) {
                            Drawable numberText = null;
                            if (badgeNumbers[menuIndex] > 0) {
                                int height = mTextView.getHeight();
                                numberText = new TextDrawable(badgeNumbers[menuIndex], mTextView.getTextSize(),
                                        mTextView.getCurrentTextColor(), height);
                                numberText.setBounds(0, 0, height, height);
                            }

                            // Similar to NavigationMenuItemView.setIcon
                            Drawable icon = drawerMenu.getItem(menuIndex).getIcon();
                            Drawable.ConstantState state = icon.getConstantState();
                            icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
                            int mIconSize = navigationView.getContext().getResources().getDimensionPixelSize(
                                    android.support.design.R.dimen.design_navigation_icon_size);
                            icon.setBounds(0, 0, mIconSize, mIconSize);
                            DrawableCompat.setTintList(icon, navigationView.getItemIconTintList());
                            TextViewCompat.setCompoundDrawablesRelative(mTextView, icon, null, numberText,
                                    null);
                        }
                    }
                }
            }
    }
}

From source file:com.achep.base.ui.fragments.dialogs.PermissionsDialog.java

@NonNull
@Override/*from w  ww .j  ava  2  s. c  o  m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Context context = getActivity();
    assert context != null;

    MaterialDialog md = new MaterialDialog.Builder(context).title(R.string.permissions_dialog_title)
            .items(new CharSequence[] { "", "" }).negativeText(R.string.later).build();

    // Make title more red
    TextView title = md.getTitleView();
    title.setTextColor(title.getCurrentTextColor() & 0xFFFF3333 | 0xFF << 16);

    ListView listView = md.getListView();
    assert listView != null;
    mAdapter = new PermissionAdapter(context, new ArrayList<Permission>());
    listView.setAdapter(mAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            PermissionAdapter adapter = (PermissionAdapter) parent.getAdapter();
            Permission item = adapter.getItem(position);

            try {
                startActivity(item.getIntentSettings());
            } catch (ActivityNotFoundException e) {
                int msg = item.getErrorResource();
                if (msg != 0)
                    ToastUtils.showLong(getActivity(), msg);
            }
        }
    });

    return md;
}

From source file:com.achep.acdisplay.ui.fragments.dialogs.SetupPermissionsDialog.java

@NonNull
@Override/*from   w  ww .  ja  v  a2  s  . c o  m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Context context = getActivity();
    assert context != null;

    View view = new DialogBuilder(context).setTitle(R.string.permissions_dialog_title)
            .setView(R.layout.dialog_permissions).createSkeletonView();

    // Make title more red
    TextView title = (TextView) view.findViewById(R.id.title);
    title.setTextColor(title.getCurrentTextColor() & 0xFFFF3333 | 0xFF << 16);

    ListView listView = (ListView) view.findViewById(R.id.list);
    mAdapter = new Adapter(context, new ArrayList<Item>());
    listView.setAdapter(mAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Adapter adapter = (Adapter) parent.getAdapter();
            Item item = adapter.getItem(position);
            item.run();
        }
    });

    return new AlertDialog.Builder(context).setView(view).setNeutralButton(R.string.later, null).create();
}

From source file:com.nttec.everychan.ui.theme.CustomThemeHelper.java

@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
    int mappingCount = 0;
    if (attrs != null) {
        for (int i = 0, size = attrs.getAttributeCount(); i < size; ++i) {
            String value = attrs.getAttributeValue(i);
            if (!value.startsWith("?"))
                continue;
            int attrId = resources.getIdentifier(value.substring(1), null, null); //Integer.parseInt(value.substring(1));
            if (attrId == 0) {
                Logger.e(TAG, "couldn't get id for attribute: " + value);
                continue;
            }/* www  . j  a va 2s  .  co  m*/
            int index = customAttrs.indexOfKey(attrId);
            if (index >= 0) {
                mappingKeys[mappingCount] = attrs.getAttributeNameResource(i);
                mappingValues[mappingCount] = customAttrs.valueAt(index);
                ++mappingCount;
            }
        }
    }

    if (mappingCount == 0 && textColorPrimaryOverridden == textColorPrimaryOriginal)
        return null;

    View view = instantiate(name, context, attrs);
    if (view == null)
        return null;

    boolean shouldOverrideTextColor = textColorPrimaryOverridden != textColorPrimaryOriginal
            && view instanceof TextView;
    for (int i = 0; i < mappingCount; ++i) {
        switch (mappingKeys[i]) {
        case android.R.attr.background:
            view.setBackgroundColor(mappingValues[i]);
            break;
        case android.R.attr.textColor:
            if (view instanceof TextView) {
                ((TextView) view).setTextColor(mappingValues[i]);
                shouldOverrideTextColor = false;
            } else {
                Logger.e(TAG, "couldn't apply attribute 'textColor' on class " + name
                        + " (not instance of TextView)");
            }
            break;
        case android.R.attr.divider:
            if (view instanceof ListView) {
                ListView listView = (ListView) view;
                int dividerHeight = listView.getDividerHeight();
                listView.setDivider(new ColorDrawable(mappingValues[i]));
                listView.setDividerHeight(dividerHeight);
            } else {
                Logger.e(TAG,
                        "couldn't apply attribute 'divider' on class " + name + " (not instance of ListView)");
            }
            break;
        default:
            String attrResName = null;
            try {
                attrResName = resources.getResourceName(mappingKeys[i]);
            } catch (Exception e) {
                attrResName = Integer.toString(mappingKeys[i]);
            }
            Logger.e(TAG, "couldn't apply attribure '" + attrResName + "' on class " + name);
        }
    }

    if (shouldOverrideTextColor) {
        TextView tv = (TextView) view;
        if (tv.getCurrentTextColor() == textColorPrimaryOriginal) {
            tv.setTextColor(textColorPrimaryOverridden);
        }
    }

    return view;
}