Reputation: 379
I have an Edittext in my app and I want user not to enter first character as whitespace..but after entering other character user can enter space, how can i do that> I have this code:
android:id="@+id/referralCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:background="@drawable/formfield_1"
android:hint="@string/referralcode_hint"
android:imeOptions="actionNext"/>
And in the java class I have an input filter:
public static InputFilter alphabetsFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source.equals("")) { // for backspace
return source;
}
if (source.toString().matches("[a-zA-Z ]+")) {
return source;
}
return "";
}
};
that doesnt allow first whitespace character, but I want it to allow entering numbers and special characters
Upvotes: 1
Views: 95
Reputation: 1641
what you can do is set addTextChangedListener
like this:
yourEditText.addTextChangedListener(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) {
if(!(s.toString().charAt(0) == ' ')) {
//do whatever you want
//you can show an alert dialog saying the first character should not be a whitespace
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Upvotes: 1
Reputation: 1485
You can make CustomEditText class like this...
public class CustomEditText extends EditText {
public CustomEditText(Context context) {
super(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
if (text.length() == 1) {
if (Character.isSpaceChar(text.charAt(0))) {
this.setText("");
}
}
}
}
and instead of EditText in xml use this CustomEditText
Upvotes: 1
Reputation: 2700
Loop in input filer, and add required chars Like :
private static class AlphaNumericInputFilter implements InputFilter {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
// Only keep characters that are alphanumeric
StringBuilder builder = new StringBuilder();
for (int i = start; i < end; i++) {
char c = source.charAt(i);
if (Character.isLetterOrDigit(c) || !Character.isSpaceChar(c)) {
builder.append(c);
}
}
// If all characters are valid, return null, otherwise only return the filtered
// characters
boolean allCharactersValid = (builder.length() == end - start);
return allCharactersValid ? null : builder.toString();
}
}
And create a new Instance of this filter, add it to edittext.
Upvotes: 0
Reputation: 1442
Can you try this :
inal EditText editText = (EditText)findViewById(R.id.editText);
InputFilter filter = new InputFilter() {
boolean canEnterSpace = false;
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if(editText.getText().toString().equals(""))
{
canEnterSpace = false;
}
StringBuilder builder = new StringBuilder();
for (int i = start; i < end; i++) {
char currentChar = source.charAt(i);
if (Character.isLetterOrDigit(currentChar) || currentChar == '_') {
builder.append(currentChar);
canEnterSpace = true;
}
if(Character.isWhitespace(currentChar) && canEnterSpace) {
builder.append(currentChar);
}
}
return builder.toString();
}
};
editText.setFilters(new InputFilter[]{filter});
Upvotes: 0