This solution also makes my field mandatory for example, "You must add your name here." In my case, I am looking for a solution in which I have a name field, but I only want to prevent the user from entering empty spaces, no matter how much they want to fill in their name or not, but they cannot enter empty spaces.
So below code works for me
public static boolean checkForEmptySpaces(String value){ if (value.length()>0 && value.trim().matches("")) { showToast(v.getContext(), "Empty spaces are not allowed!"); return false; }else { return true; } }
B.shruti
source share