Edit Text Remove Word on text Changed

Viewed 102

I have edittext like below

<com.myapp.example.customview.socialView.SocialEditText
    android:id="@+id/edt_des"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:afterTextChanged="@{(editable)->viewModel.onDescriptionTextChanged(editable)}"
    android:background="@drawable/bg_white_corner_15"
    android:backgroundTint="@color/colorPrimaryDark"
    android:elevation="5dp"
    android:fontFamily="@font/popins_reguler"
    android:gravity="top"
    android:hint="@string/upload_description"
    android:maxLength="175"
    android:padding="10dp"
    android:textColor="@color/light_white"
    android:textColorHint="@color/color_text_light"
    android:textCursorDrawable="@xml/cursor"
    android:textSize="12dp"
    app:socialFlags="hashtag" />

and in viewmodel function for get text like below

public void onDescriptionTextChanged(CharSequence text) {
    postDescription = text.toString();
    position.set(postDescription.length());
}

I have one function which can remove the word from String like below

public void main(String[] args) {
    String word = postDescription;
    String remove = "#WordForRemove";
    finalDescription = removeWords(word, remove);
}

public static String removeWords(String word ,String remove) {
    return word.replace(remove,"");
}

I want to use this function with onDescriptionTextChanged so I can remove unacceptable words from edit text, Let me know if anyone here can help me for achieving it.

Thanks!

1 Answers

On the question :

  • You have not specified what the SocialEditText view is. It looks like a custom class in your app, so nobody on StackOverflow can really know what it does. I will assume it's just an EditText, but if that class has any logic it may cause issues with what you are trying to do.
  • onDescriptionTextChanged is not a standard method in Android. Of course you can have a method with this name, but Android won't particularly pick it up.

Answer :
You simply want to remove the words when you validate the text. This will let you check whether there were any changes and possibly prompt the user about the changes. You can just run your function whenever you app accepts the input – you probably have a button for this or something (but you did not specify).

You do not want to remove text while the user is typing for the following reasons :

  • As a user, it is very annoying to have the app change what you type while you're typing it.
  • It will prevent your users to type acceptable words that happen to start with the same characters as an unacceptable word. You can't touch-type "cumulative" if the app removes these three first chars as soon as you type them (or at least you have to be cunning about it).
  • If you remove text while they are typing, you're fighting against the keyboard and its composition span. This is particularly bad in CJK languages, but is also very annoying with alphabetic keyboards. Many keyboards don't handle this well.

Note that technically it's possible to remove stuff as users are typing using the addAfterTextChangedListener method on the EditText to run your removing code. But as outlined above that will make for a very, very bad user experience.

Finally, keeep in mind that a list of bad words is not really an effective way of filtering language for a public site : it doesn't prevent users to write profanity and the like. It's very easy for humans to work around this by changing a character, mixing up case, adding symbols, using unicode look-alikes and the like.

Related