Reputation: 107
I am looking for an easy way to set further attributes of a TextView element based on the input set by TextView.setText().
Specifically, my code currently looks like this:
TextView payment;
BigDecimal mBigDecimal;
payment.setText(BigDecimal.toString());
if (mBigDecimal.compareTo(BigDecimal.ZERO) == -1) {
payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.lightred)));
} else if (mBigDecimal.compareTo(BigDecimal.ZERO) == 1) {
payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.lightgreen)));
} else {
payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.transparent)));
}
// ColorStateListStud only holds state_enabled and sets the given color
This is copied multiple times over the entirety of my code. so I'd like to streamline that bit best as possible. I want to set the BackgroundTintList based on the BigDecimal value, which I always have at hand when setting the text of a TextView element.
Upvotes: 0
Views: 53
Reputation: 18677
I think you have two options.
Create a custom Utils.java class
You can create a custom and static class which updates the text view for you.
public class Utils {
public static void setText(TextView textView, BigDecimal bigDecimal) {
if(textView != null && bigDecimal != null) {
// Get context
Context context = textView.getContext();
// Set text
textView.setText(bigDecimal.toString());
// Set color
if (bigDecimal.compareTo(BigDecimal.ZERO) == -1) {
textView.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(context, R.color.lightred)));
} else if (bigDecimal.compareTo(BigDecimal.ZERO) == 1) {
textView.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(context, R.color.lightgreen)));
} else {
textView.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(context, R.color.transparent)));
}
} else {
Log.e("ERROR", "Error: TextView and/or BigDecimal is null");
}
}
Then, you can just call:
Utils.setText(mTextView, mBigDecimal);
Create your own Custom TextView
public class CustomTextView extends TextView {
public CustomTextView(final Context context) {
this(context, null);
}
public CustomTextView(final Context context,
@Nullable final AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextView(final Context context, @Nullable final AttributeSet attrs,
final int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setText(BigDecimal bigDecimal) {
setText(bigDecimal.toString());
// Set color
if (bigDecimal.compareTo(BigDecimal.ZERO) == -1) {
setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(getContext(), R.color.lightred)));
} else if (bigDecimal.compareTo(BigDecimal.ZERO) == 1) {
setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(getContext(), R.color.lightgreen)));
} else {
setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(getContext(), R.color.transparent)));
}
}
}
Then, in the java side:
CustomTextView mTextView = (CustomTextView) findViewById(R.id.text_view);
mTextView.setText(mBigDecimal);
And in your layout.xml:
<com.test.CustomTextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Upvotes: 1
Reputation: 140
Try setText()
call at the end
if (mBigDecimal.compareTo(BigDecimal.ZERO) == -1) {
payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.lightred)));
} else if (mBigDecimal.compareTo(BigDecimal.ZERO) == 1) {
payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.lightgreen)));
} else {
payment.setBackgroundTintList(new ColorStateListStud().getList(ContextCompat.getColor(super.getContext(), R.color.transparent)));
}
// Only then, call setText() here
payment.setText(BigDecimal.toString());
Upvotes: 0