Force a View to redraw itself

Viewed 67196

I have created a custom View (let's call it MyView) which basically just draws some text on itself using the canvas. The text to be drawn is set using a global variable.

At some point during the program's execution, I want to change the global variable, and have the MyView redraw itself to update the text. I tried findViewById() and then invalidate(), but this does nothing. I suspect that since nothing within the MyView has changed, it thinks it has no reason to call onDraw(). Is there any way to force a View to redraw itself even if it thinks it doesn't need to?

3 Answers

Example:

customView.set(...)
customView.requestLayout();
customView.invalidate();

Reference: android.widget.TextView#onConfigurationChanged

@Override
protected void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (!mLocalesChanged) {
        mTextPaint.setTextLocales(LocaleList.getDefault());
        if (mLayout != null) {
            nullLayouts();
            requestLayout();
            invalidate();
        }
    }
    if (mFontWeightAdjustment != newConfig.fontWeightAdjustment) {
        mFontWeightAdjustment = newConfig.fontWeightAdjustment;
        setTypeface(getTypeface());
    }
}
Related