Reputation: 23
I want to make a textView
that turns green with a positive number, red with a negative number, and goes invisible when 0. I have two separate drawables for each background.
I have changed updateView.setDrawable(@Drawable/add_background)
to updateView.setDrawable(R.drawable.add_background)
, and am now getting the error 'setBackground(android.graphics.drawable.Drawable)' in 'android.view.View' cannot be applied to '(int)'
Here is the code:
Integer team1UpdateAmount = 0;
TextView updateView = findViewById(R.id.team1UpdateView);
if (team1UpdateAmount == 0) {updateView.setVisibility(View.INVISIBLE); return;}
updateView.setVisibility(View.VISIBLE);
if (team1UpdateAmount > 0) {
updateView.setText("+" + team1UpdateAmount);
updateView.setBackground(R.drawable.add_background);
}
if (team1UpdateAmount < 0) {
updateView.setText("" + team1UpdateAmount);
updateView.setBackground(R.drawable.sub_background);
}
Upvotes: 0
Views: 50
Reputation: 19253
check out this line
updateView.setBackgroundResource(R.drawable.sub_background)
thats how you are referencing to resources in code, referencing by @
is for XMLs
Upvotes: 1
Reputation: 23
While experimenting, I found the answer.
I set getResources().getDrawable(R.drawable.add_background)
to a variable called addBackground
(same with sub), and referred to that variable in updateView.setBackground(addBackground)
The code now looks like this:
Drawable addBackground = getResources().getDrawable(R.drawable.add_background);
Drawable subBackground = getResources().getDrawable(R.drawable.sub_background);
TextView updateView = findViewById(R.id.Team1UpdateView);
if (team1UpdateAmount == 0) {updateView.setVisibility(View.INVISIBLE); return;}
updateView.setVisibility(View.VISIBLE);
if (team1UpdateAmount > 0) {
updateView.setText("+" + team1UpdateAmount);
updateView.setBackground(addBackground);
}
if (team1UpdateAmount < 0) {
updateView.setText("" + team1UpdateAmount);
updateView.setBackground(subBackground);
}
Upvotes: 0