0

I have written a simple code that Toast a message whenever the user types a particular word, for example, "help" and then it Toast a message

public void onAccessibilityEvent(AccessibilityEvent event) {
    switch (event.getEventType()) {
        case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
    
            String string = event.getText().toString();
            if (string == "help") {
                Toast.makeText(getApplicationContext(), "we are here to help", Toast.LENGTH_SHORT).show();
            }

I think the problem here is in the string as it is logging unnecessary words also as user types, is there any way how we can initialize the string for that particular word?

HoRn
  • 1,458
  • 5
  • 20
  • 25
  • 3
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – David Conrad Aug 15 '22 at 15:36

1 Answers1

1

Try this

String string = event.getText().toString();

if (string.toLowerCase().indexOf("help") != -1) {
     Toast.makeText(getApplicationContext(), "we are here to help", Toast.LENGTH_SHORT).show();
}
HoRn
  • 1,458
  • 5
  • 20
  • 25
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
  • how can I add different words to it for example if I want to add a please word with the help word in it – shohel hossain Aug 15 '22 at 14:57
  • it worked but, after I wrote "help" it toasted the massage but after that, with every other alphabet it keeps toasting the massage, can you please help – shohel hossain Aug 19 '22 at 06:22