Home > database >  how to start an android activity when ever a user types a particular words
how to start an android activity when ever a user types a particular words

Time:07-28

I have made an android app that has foreground service and accessibility service in it now I want to start a class where when every user types a particular word example help then it will start that activity

CodePudding user response:

You can do something like this :

KOTLIN

editText.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence, i: Int, i1: Int, i2: Int) {}
                override fun onTextChanged(s: CharSequence, i: Int, i1: Int, i2: Int) {}

                override fun afterTextChanged(s: Editable) {
                    if (s.isNotEmpty()) {
                        if (s.toString().equals("help", ignoreCase = true)) {
                            startActivity(Intent(this, YourActivity::class.java))
                        }
                    }
                }
            })

JAVA

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null && editable.toString().length() >0 ) {
                    startActivity(context, YourActivity.class);
                }
            }
        });

CodePudding user response:

You should use below code, if it works then enjoy it otherwise send your code i'll help you.

    binding.etName.doAfterTextChanged {
        if(it.toString() == "particular_word") {
            val videoPlayIntent = Intent(requireActivity(), PlaybackActivity::class.java)
            startActivity(videoPlayIntent)
        }
    }
  • Related