92

My code looks like this:

    TextView task_text = (TextView) view.findViewById(R.id.task_text);
    task_text.setPaintFlags( task_text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

This causes a strike through effect to appear on the text. However, I'd like to know how to remove the flag once set, and how to detect that the flag is set.

I understand this is a bitwise operation, but I've tried both ~ and - operators, neither work.

James
  • 6,471
  • 11
  • 59
  • 86

10 Answers10

194

To remove a flag, this should work:

task_text.setPaintFlags( task_text.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));

Which means set all the set flags, except of Paint.STRIKE_THRU_TEXT_FLAG.

To check if a flag is set (Edit: for a moment I forgot it is java...):

if ((task_text.getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) > 0)
MByD
  • 135,866
  • 28
  • 264
  • 277
31

In Kotlin

task_text.paintFlags = task_text.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
  • Not sure why it doesn't work for me! `tvPhone.autoLinkMask = Linkify.ALL`, then `tvPhone.text = "1-800-12345"`, then `tvPhone.paintFlags = tvPhone.paintFlags and Paint.UNDERLINE_TEXT_FLAG.inv()` – Dr.jacky Oct 22 '20 at 11:34
29

This also works:

task_text.setPaintFlags(0);
Pang
  • 9,564
  • 146
  • 81
  • 122
arun-r
  • 3,104
  • 2
  • 22
  • 20
  • 2
    I think this is a strange case where not using the correct is more corret, cause there is no reason to check for conditions here, you just want 2 cases, set the Paint.Flag or to remove it, great solution. – cutiko Dec 02 '15 at 19:58
  • 5
    Don't do this. This removes ALL flags, not just a particular one. – Michael Peterson Oct 26 '17 at 16:21
10

Use exclusive OR operator ^ instead of | with &(~) combination:

// setup STRIKE_THRU_TEXT_FLAG flag if current flags not contains it
task_text.setPaintFlags(task_text.getPaintFlags() ^ Paint.STRIKE_THRU_TEXT_FLAG));

// second call will remove STRIKE_THRU_TEXT_FLAG
task_text.setPaintFlags(task_text.getPaintFlags() ^ Paint.STRIKE_THRU_TEXT_FLAG));

Check if flag is currently setup:

if((task_text.getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) == Paint.STRIKE_THRU_TEXT_FLAG)
Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
  • Thank you @Sergey This is the only robust answer here. 1) Check if the flag you want to remove is present. 2) If it is, remove it using an XOR. I wish users would stop upvoting answers that involve setting the paintFlags to 0 or using XOR before checking if the flag to remove is present. – Michael Peterson Oct 26 '17 at 16:29
7

In my opinion, just set its default flag is a better choice. Otherwise, the text will be looked jagged. The default flag in TextView (EditText extends TextView) is

Paint.ANTI_ALIAS_FLAG

And set a new paintflag will replace the previous one. I have made a test to verify it. So, just like this:

task_text.setPaintFlags(Paint.ANTI_ALIAS_FLAG);
sanmianti
  • 175
  • 2
  • 8
6

|--------------------------------------------------|
|<*>| Underline with a textView :
|--------------------------------------------------|

|*| Add Underline :

 txtVyuVar.setPaintFlags(txtVyuVar.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

|*| Remove Underline :

txtVyuVar.setPaintFlags(txtVyuVar.getPaintFlags() ^ Paint.UNDERLINE_TEXT_FLAG);

|*| Check Underline :

if((txtVyuVar.getPaintFlags() & Paint.UNDERLINE_TEXT_FLAG) == Paint.UNDERLINE_TEXT_FLAG)
{
    // Codo Todo
}

|*| Toggle Underline :

if((txtVyuVar.getPaintFlags() & Paint.UNDERLINE_TEXT_FLAG) == Paint.UNDERLINE_TEXT_FLAG)
{
    txtVyuVar.setPaintFlags(txtVyuVar.getPaintFlags() ^ Paint.UNDERLINE_TEXT_FLAG);
}
else
{
    txtVyuVar.setPaintFlags(txtVyuVar.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
}
Sujay U N
  • 4,974
  • 11
  • 52
  • 88
0

TextView details_actual_price; //and find id from your xml file

details_actual_price.setPaintFlags(details_actual_price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 01 '22 at 12:55
0

//used for adapter calss

Textview details_actual_price; // and find id holder.actulPrice.setPaintFlags(holder.actulPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 01 '22 at 12:34
0

I tried using the above mentioned methods in kotlin using kotlin bitwise operators and I was getting weird results...

so to remove a flag I just did

view.paintflags = 0x00
SIMPLEGUY
  • 163
  • 7
0

In Kotlin:

to set "strike thru" flag:

numberTextView?.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG

to remove this flag:

numberTextView?.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG and Paint.STRIKE_THRU_TEXT_FLAG.inv()
dumbfingers
  • 7,001
  • 5
  • 54
  • 80
ledenda
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 11 '22 at 17:14