0

In my application, I create custom TextView and I change colors in Java file.
But I want to change this colors with XML file such as some Android libraries.

My Java code for custom TextView:

public class GradientTextView extends 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 the layout is changed
        if (changed) {
            getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(),
                    ContextCompat.getColor(getContext(), R.color.gradientTextColor2),
                    ContextCompat.getColor(getContext(), R.color.tabCategoryColor),
                    Shader.TileMode.MIRROR));
        }
    }
}

I want change this parameter in XML code :

ContextCompat.getColor(getContext(), R.color.gradientTextColor2),
ContextCompat.getColor(getContext(), R.color.tabCategoryColor),

My XML code:

<com.mcc.wpnews.view.GradientTextView
    android:id="@+id/navHeader_notVipBuyTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:text="@string/buyVip"
    android:textSize="@dimen/font16" />

How can do achieve this, or is it something that I am missing? Please help me

Vaibhav Dhoke
  • 459
  • 6
  • 14
RedBounce
  • 213
  • 5
  • 14
  • https://developer.android.com/training/custom-views/create-view.html – pskink Apr 02 '18 at 06:23
  • @pskink, can you help me with my codes? because I am amateur. please – RedBounce Apr 02 '18 at 06:27
  • did you read the link i posted? if so, whats unclear? – pskink Apr 02 '18 at 06:27
  • Apply the `declare-styleable` part from above link . – ADM Apr 02 '18 at 06:30
  • @pskink, how to change `if (changed) { getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(), ContextCompat.getColor(getContext(), R.color.gradientTextColor2), ContextCompat.getColor(getContext(), R.color.tabCategoryColor), Shader.TileMode.MIRROR)); }` with `R.styleable` code? please help me – RedBounce Apr 02 '18 at 06:34
  • @ADM, can you send to me code with my codes? really I am amateur and need your help – RedBounce Apr 02 '18 at 06:34
  • read `Apply Custom Attributes` section, read the whole page anyway, how do you expect to get some knowledge without no learning? – pskink Apr 02 '18 at 06:36
  • I don't think that's necessary the link provided by pskink has all the info and code for creating custom view. Read it throughout and apply the same . – ADM Apr 02 '18 at 06:36

0 Answers0