You can use a regular expression to do that. Use code like this one:
if(Pattern.matches("^\\w+\\s\\w+\\s\\w+$", mSearchView.getText().toString()))
Also make sure to check if mSearchView.getText()
is not null - you probably will get a NullReferenceException
with a blank EditText
content.
In the end you may want to create a method like this one:
public static boolean containsTwoSpaces(Editable text) {
if (text == null) return false;
return Pattern.matches("^\\w+\\s\\w+\\s\\w+$", text.toString());
}
just for convenience, clearance and making sure you don't bump into a NullPointerException
.