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.

10 Answers 10

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)
1
  • 3
    Another easy solution is simply to use the various methods provided on Paint :)
    – Romain Guy
    Jul 23, 2011 at 7:48
32

In Kotlin

task_text.paintFlags = task_text.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
1
  • 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, 2020 at 11:34
29

This also works:

task_text.setPaintFlags(0);
2
  • 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 2, 2015 at 19:58
  • 6
    Don't do this. This removes ALL flags, not just a particular one. Oct 26, 2017 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)
1
  • 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. Oct 26, 2017 at 16:29
8

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);
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);
}
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);

1
  • 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.
    – Community Bot
    Feb 1, 2022 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
  • 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.
    – Community Bot
    Feb 1, 2022 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
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()
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.
    – Community Bot
    Mar 11, 2022 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.