How to open a dialog window on press of @ key in Android?

Movies FUn :

This the code that I've written but I am getting the full string and the function is only triggered if the first character is @. I want it to check for every instance of @ and whenever the @ key is pressed i want to open the dialog. I tried using the onKeyDown() but couldn't make it work.

override fun afterTextChanged(editable: Editable?) {
viewModel.onDoubtTextChanged(editable.toString())
if (editable.toString() == "@") {
openUserSuggestionDialog(doubtDescription.users as ArrayList<User>,viewModel)
}

editable contains the value which is being inputted into the TextView

whiterabbitj :

Add a on TextChange Listener to your editText

 editable.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //get the last String entered
                String lala = editable.getText().toString();
                String subChar = lala.substring(lala.length() - 1);
                     if(subChar.equals("@"))
                      openUserSuggestionDialog(doubtDescription.users as ArrayList<User>,viewModel)


            }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=175309&siteId=1