0

Is there logic that we apply if else on Text of TextView without using integer?

public class MainActivity extends AppCompatActivity {

    TextView text;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text=findViewById(R.id.textView1);
        button=findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                if (text!=Paint.STRIKE_THRU_TEXT_FLAG)
                {
                    text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                }
                else
                {
                    text.setPaintFlags(text.getPaintFlags() | ~ Paint.STRIKE_THRU_TEXT_FLAG);
                }
            }
        }
        );

    }
}

I want to do that, on button click, if the text is normal it will become Strikethrough and if the text is Strikethrough it will become normal.

  • Are you sure you want to use `!=` to compare the `TextView`s? – Turing85 Jul 02 '19 at 20:18
  • Yeah there was some little bit mistake in logic, now i correct it, but i want to do the same thing without using any integer value... do u have any other logic to compare TextView rather than == ??? – Mubashir Ayub Jul 02 '19 at 20:23
  • Please see: [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) (this actually applies to all non-primitives in Java) – Turing85 Jul 02 '19 at 20:24
  • I appreciate your link, but can u plz do correction in my IF ELSE statement in my code??? – Mubashir Ayub Jul 02 '19 at 20:27

1 Answers1

0

If you want to toggle it, try

text.setPaintFlags(text.getPaintFlags() ^  Paint.STRIKE_THRU_TEXT_FLAG);

No if needed.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127