Search code examples
androidtextviewgradientandroid-custom-view

Gradient text color in a custom view


I have this GradientTextView class that extends AppCompatTextView

public class GradientTextView extends android.support.v7.widget.AppCompatTextView {

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

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

public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);

    //Setting the gradient if layout is changed
    if (changed) {
        getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(),
                ContextCompat.getColor(getContext(), R.color.colorStart),//Red Color
                ContextCompat.getColor(getContext(), R.color.colorEnd),// Blue Color
                Shader.TileMode.CLAMP));
    }
}

}

and here is the xml of gradient textview

<package.GradientTextView
        android:id="@+id/textLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login."
        android:textStyle="bold"
        android:padding="4dp"
        android:layout_marginStart="8dp"/>

Right now I have a textview with a red and blue gradient. I would like to change the gradient color dinamically.

What's the best way to change the color programatically?

After answer of @Nicola Gallazzi I am updating my question Now new file of GradientTextView is

public class GradientTextView extends android.support.v7.widget.AppCompatTextView {


int colorStart = ContextCompat.getColor(getContext(), R.color.colorStart);
int colorEnd = ContextCompat.getColor(getContext(), R.color.colorEnd);

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

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

public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    //Setting the gradient if layout is changed
    if (changed) {
        getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(), colorStart, colorEnd,
                Shader.TileMode.CLAMP));
    }
}

public void setGradientColors(int colorStart, int colorEnd) {
    this.colorStart = colorStart;
    this.colorEnd = colorEnd;
    // this forces view redrawing
    invalidate();
}

}

but here is a new problem that is when I am using this in activity like @Nicola Gallazzi is shows error.

CODE

        textView.setGradientColors(ContextCompat.getColor(this,R.color.ncolorEnd, R.color.ncolorStart));

ERROR

Here you can find the error which


Solution

  • You should define your colorStart and colorEnd as instance variables of your GradientTextView class. Then, define a public method within the class to set the colorStart and the colorEnd programmatically:

    public class GradientTextView extends android.support.v7.widget.AppCompatTextView {
    
        private int colorStart = ContextCompat.getColor(getContext(), R.color.colorPrimary);
        private int colorEnd = ContextCompat.getColor(getContext(), R.color.colorAccent);
    
    
        public GradientTextView(Context context) {
            super(context);
        }
    
        public GradientTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
        }
    
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
            //Setting the gradient if layout is changed
            if (changed) {
                getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(), colorStart, colorEnd,
                        Shader.TileMode.CLAMP));
            }
        }
    
        public void setGradientColors(int colorStart, int colorEnd) {
            this.colorStart = colorStart;
            this.colorEnd = colorEnd;
            // this forces view redrawing
            invalidate();
        }
    
    }