Reputation: 439
I am developing my first android app and it is a very basic todo list app. I have used SQLite database to store the tasks. The added taskes are displayed on the main activity.
I want the user to be able to click on any task to mark it done, and hence the task must change its color to red.
If the user clicks the same task again, it should revert back to it's original color.
I tried to add an onclick listener like this :
public void onTaskDone(View view){
view.setTextColor(Color.RED);
}
but the setTextColor method appears in red (that means it can't be done like this).
Please Help !!
Upvotes: 0
Views: 1011
Reputation: 333
In a single textview click listener we can change the color like below.i having the adapter class i want to click the particular position textview need to change the color when i click....
holder.statusText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.statusText.getCurrentTextColor() == context.getResources().getColor(R.color.colorSkyBlue)) {
holder.statusText.setTextColor(Color.BLACK);
} else {
holder.statusText.setTextColor(context.getResources().getColor(R.color.colorSkyBlue));
}
}
});
Its really worked for me.
Upvotes: 1
Reputation: 2246
boolean isSelected =false;
public void onTaskDone(TextView view){
view.setTextColor(isSelected?Color.BLACK:Color.RED);
isSelected = !isSelected;
}
Upvotes: 0
Reputation: 466
You can simply achieve this by adding onclick Listener and in that you can change the text color
tvStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(tvStatus.getCurrentTextColor()==Color.BLACK)
tvStatus.setTextColor(Color.RED);
}else{
tvStatus.setTextColor(Color.BLACK);
}
}
});
Upvotes: 3
Reputation: 230
boolean isSelected =false
public void onTaskDone(View view){
if(!isSelected){
view.setTextColor(Color.RED);
isSelected = true;
}else{
view.setTextColor(Color.GREEN);
isSelected = false;
}
}
Upvotes: 3