How to make programmatically floating point (Android)
I am creating a custom TextView (Java class) and I am having problem "translate" a string (to "original TextView" xml)
android:background="@drawable/myDrawableShape"
in java void to change the color of "myDrawableShape"
myDrawableShape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffafafaf" />
<corners android:radius="15dp" />
I will get the color to set from String, void to change color programmatically could be (for example)
void colorSet(String color)
Thanks in advance!
+3
source to share
2 answers
Then you can create your shape in Java using the 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;
}
+5
source to share
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);
+1
source to share