Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7309175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T00:04:42+00:00 2026-05-29T00:04:42+00:00

Here is the custom class of GradientTextView provided by @koush (source is there on

  • 0

Here is the custom class of GradientTextView provided by @koush (source is there on (github)
Now I got the class but how to call this class anyone can help since I am new bee on android
As far as I understood it can use custom attribute to but nyways how to call this class from MainActivity to get run. I dont know how to send AttributeSet parameters. it’s too confusing

package android.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.LinearGradient;
import android.graphics.Shader.TileMode;
import android.text.BoringLayout;
import android.util.AttributeSet;
import android.widget.TextView;

public class GradientTextView extends TextView {
    public GradientTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    int mStartColor = 0;
    int mEndColor = 0;
    float mAngle;
    String mText;
    BoringLayout mLayout;

    public GradientTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        int[] ids = new int[attrs.getAttributeCount()];
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            ids[i] = attrs.getAttributeNameResource(i);
        }

        TypedArray a = context.obtainStyledAttributes(attrs, ids, defStyle, 0);

        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            String attrName = attrs.getAttributeName(i);
            if (attrName == null)
                continue;

            if (attrName.equals("startColor")) {
                mStartColor = a.getColor(i, -1);
            }
            else if (attrName.equals("endColor")) {
                mEndColor = a.getColor(i, -1);
            }
            else if (attrName.equals("angle")) {
                mAngle = a.getFloat(i, 0);
            }
        }
    }

    public static void setGradient(TextView tv, float angle, int startColor, int endColor) {
        tv.measure(tv.getLayoutParams().width, tv.getLayoutParams().height);
        LinearGradient gradient = getGradient(tv.getMeasuredWidth(), tv.getMeasuredHeight(), angle, startColor, endColor);
        tv.getPaint().setShader(gradient);
    }

    static LinearGradient getGradient(int measuredWidth, int measuredHeight, float angle, int startColor, int endColor) {
        // calculate a vector for this angle
        double rad = Math.toRadians(angle);
        double oa = Math.tan(rad);
        double x;
        double y;
        if (oa == Double.POSITIVE_INFINITY) {
            y = 1;
            x = 0;
        }
        else if (oa == Double.NEGATIVE_INFINITY) {
            y = -1;
            x = 0;
        }
        else {
            y = oa;
            if (rad > Math.PI)
                x = -1;
            else
                x = 1;
        }

        // using the vector, calculate the start and end points from the center of the box
        int mx = measuredWidth;
        int my = measuredHeight;
        int cx = mx / 2;
        int cy = my / 2;

        double n;
        if (x == 0) {
            n = (double)cy / y;  
        }
        else if (y == 0) {
            n = (double)cx / x;
        }
        else {
            n = (double)cy / y;
            double n2 = (double)cx / x;
            if (Math.abs(n2) < Math.abs(n))
                n = n2;
        }

        int sx = (int)(cx - n * x);
        int sy = (int)(cy - n * y);
        int ex = (int)(cx + n * x);
        int ey = (int)(cy + n * y);

        return new LinearGradient(sx, sy, ex, ey, startColor, endColor, TileMode.CLAMP);
    }

    protected void onDraw(android.graphics.Canvas canvas) {
        if (mGradient == null) {
            mGradient = getGradient(getMeasuredWidth(), getMeasuredHeight(), mAngle, mStartColor, mEndColor);
            getPaint().setShader(mGradient);
        }
        super.onDraw(canvas);
    }

    public int getStartColor() {
        return mStartColor;
    }

    public void setStartColor(int startColor) {
        mStartColor = startColor;
        invalidate();
    }

    public int getEndColor() {
        return mEndColor;
    }

    public void setEndColor(int endColor) {
        mEndColor = endColor;
        invalidate();
    }


    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mGradient = null;
    }
    LinearGradient mGradient;
}

This custom class are too confusing but they are used for tweaking like outline, gradients, and etc I am very keen to understand the flow

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T00:04:47+00:00Added an answer on May 29, 2026 at 12:04 am

    you can use this “View Element” by instanciating/using a view object:

    TextView myView = new TextView (this);
    myView.setText("Test");
    GradientTextView.setGradient(myView, 90, Color.BLACK, Color.WHITE);   
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            Layoutparams.WRAP_CONTENT);
    
    myLayout.addView(myView, params);
    

    You can even use this object using it your layouts XML file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, here's the situation: I've got... a custom class with an UIImageView -property, let's
This is my code here the variable bq is a custom class called BasicQuery
Here is my custom class that I have that represents a triangle. I'm trying
Is it possible to cast a custom class to a value type? Here's an
i'm missing something fundamental here. i have a very simple custom class that draws
Here is my custom class: PolygonShape : NSObject { int numberOfSides; int minimumNumberOfSides; int
Here is my custom logger class, which helps me create logs file in my
I'm trying to create a custom class for my pages in my project. Here's
I create a custom class which I call MyClass in a module MyModule module
I am using a custom class based off RelativeLayout but it obviously doesn't render

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.