3

i'm making a custom TextView (Java class) and i'm having trouble "to translate" the line (on "original TextView" xml)

android:background="@drawable/myDrawableShape"

to a java void to change the color of the "myDrawableShape"

myDrawableShape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffafafaf" />
<corners android:radius="15dp" />

I'll get the color to set from a String, the void to change the color programmatically could be (for example)

void colorSet(String color)

Thanks in advance!

Cœur
  • 37,241
  • 25
  • 195
  • 267
ieselisra
  • 387
  • 4
  • 16
  • I'm having trouble understanding your questions. Are you asking how to set a TextView's background programmatically to a drawable? If so, this would be the code (ContextCompat is part of the v4 support library): textView.setBackground(ContextCompat.getDrawable(MyActivity.this, R.drawable.myDrawableShape)); – tim.paetz Jun 07 '17 at 00:50
  • Explained in [this](https://stackoverflow.com/questions/5940825/android-change-shape-color-in-runtime) post. See the first answer. – Pavel Bochkarev Jun 07 '17 at 00:57
  • Sorry for my english!! No, i want to change the "android:color="#ffafafaf" " on the XML drawable from the code. So like it now works, i need a xml for each color. What i want is to change the color from the code when creating the textview from a customTextview class, something like createText(String color) – ieselisra Jun 07 '17 at 04:52

2 Answers2

10

You can then create your Shape Drawable in Java itself using below code.

public Drawable getRoundRect() {
    RoundRectShape rectShape = new RoundRectShape(new float[]{
            10, 10, 10, 10,
            10, 10, 10, 10
    }, null, null);

    ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape);
    shapeDrawable.getPaint().setColor(Color.parseColor("#FF0000"));
    shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
    shapeDrawable.getPaint().setAntiAlias(true);
    shapeDrawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
    return shapeDrawable;
}
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
1

Change Color:

  TextView tv= (TextView) findViewById(R.id.txt_time);

    Drawable background = getResources().getDrawable(R.drawable.test);

    if (background instanceof ShapeDrawable) {
        // If 'ShapeDrawable'
        ShapeDrawable shapeDrawable = (ShapeDrawable) background;
        shapeDrawable.getPaint().setColor(ContextCompat.getColor(this,R.color.colorAccent));
    } else if (background instanceof GradientDrawable) {
        // If'GradientDrawable'
        GradientDrawable gradientDrawable = (GradientDrawable) background;
        gradientDrawable.setColor(ContextCompat.getColor(this,R.color.colorPrimary));
    } else if (background instanceof ColorDrawable) {
        // If ColorDrawable
        ColorDrawable colorDrawable = (ColorDrawable) background;
        colorDrawable.setColor(ContextCompat.getColor(this,R.color.colorPrimaryDark));
    }
    tv.setBackground(background);
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98