Reputation: 307
Description:
I am creating an app in which you can change your profile´s name. The problem is that users can write their names with a lot of empty spaces and I don´t know how to avoid this problem.
Example:
(I am going to represent empty spaces with underscore "_")
Edit your name: T_____omas_Men________n____i_ti //// (31 chars)
If someone writes that on an edit text, empty spaces are going to be saved and then we will see that large name into user's profile.
What do I want?
I need a code able to check when the edit text has more than one (> 1) empty spaces in a row in order to delete them. The code should be able to detect that "name" must have one empty space after "last name"
Upvotes: 0
Views: 452
Reputation: 1347
Use TextWatcher as described here: Android: How can I validate EditText input?
Then in onTextChanged(CharSequence s, int start, int before, int count) method you can validate user input, here is example :
if (s.contains(" ")) {
// ask user to edit input
// or change text programatically
String after = before.trim().replaceAll(" +", " ");
// set new text
}
Upvotes: 2
Reputation: 2223
I'm offering a different kind of solution that focuses on working with bare Strings instead of the EditText. Bear in mind that this answer works in combination with the other answers here.
private static String filterMultipleSpaces(String s) {
char[] result = new char[s.length()];
int iC = 0;
for (int iS = 0; iS < s.length(); ++iS) {
char currentChar = s.charAt(iS);
if (currentChar != ' ' || (iC > 0 && result[iC - 1] != ' ')) {
result[iC] = currentChar;
++iC;
}
}
return new String(result);
}
The above static function takes a String and removes multiple spaces. Ideally you would use this when the text changes (see @Divakar's answer) or when submitting the EditText's value if you wish
Upvotes: 0
Reputation: 1287
You can restrict EditText by using TextWatcher in tricky way.
Working flow for below code
Logic 1 : To avoid empty space as a first character
Logic 2 : Restrict EditText to allow only one space after a character
Logic 3 : Restrict EditText to allow only one space for entire string
Logic 4 : Remove Text Change Lister before setting value in editText
Logic 5 : Set proper value in editText
Logic 6 : Again add Text Change Lister for editText
Try the below code.
package com.example.application;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.R;
public class SampleActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
editText = findViewById(R.id.editText);
editText.addTextChangedListener(textWatcher);
}
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String preValue = editText.getText().toString();
/*
* To avoid empty space as a first character
* */
if (preValue.length() > 0 && preValue.trim().isEmpty()) {
editText.setText(preValue.substring(0, s.length() - 1));
editText.setSelection(editText.getText().toString().length());
}
/*
* Restrict EditText to allow only one space after a character
* */
if (preValue.endsWith(" ")) {
editText.setText(preValue.substring(0, s.length() - 1));
editText.setSelection(editText.getText().toString().length());
}
/*
* Restrict EditText to allow only one space for entire string
* */
if (preValue.trim().contains(" ") && preValue.endsWith(" ")) {
/*
* Remove Text Change Lister before setting value in editText
* */
editText.removeTextChangedListener(textWatcher);
/*
* Set proper value in editText
* */
editText.setText(preValue.substring(0, s.length() - 1));
editText.setSelection(editText.getText().toString().length());
/*
* Again add Text Change Lister for editText
* */
editText.addTextChangedListener(textWatcher);
}
if (preValue.contains("---") && preValue.endsWith(" ")) {
String[] words = preValue.split(" ");
if (words.length >= 2) {
editText.setText(preValue.substring(0, s.length() - 1));
editText.setSelection(editText.getText().toString().length());
return;
}
StringBuilder stringBuilder = new StringBuilder();
for (String word : words) {
stringBuilder.append(word).append(" ");
}
/*
* Remove Text Change Lister before setting value in editText
* */
editText.removeTextChangedListener(textWatcher);
/*
* Set proper value in editText
* */
editText.setText(stringBuilder);
editText.setSelection(editText.getText().toString().length());
/*
* Again add Text Change Lister for editText
* */
editText.addTextChangedListener(textWatcher);
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
I hope this will solve your problem.
Upvotes: 1