What's the Best Way to Limit Text Length of Edittext in Android

What's the best way to limit text length of EditText in Android

Documentation

Example

android:maxLength="10"

How to set limit for EditText in Android?

This is an answer if you want to detect if there's more than 30 characters

First of all I recommend to you to set it on your .xml file as follows :

<EditText
android:id="@+id/someId"
android:maxLength="30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

Then in your onCreate() you have to initialize it do it as follows :

EditText eText = (EditText)findViewById(R.id.someId);

Now you have to add Filters to it as follows:

eText.setFilters(new InputFilter[] { 
new InputFilter.LengthFilter(30)
});

And finally you add a TextWatcher() to deal with it as follows:

    eText.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(count >= 30) Toast.makeText(MainActivity.this, "Max length of EditText is "+ String.valueOf(30), Toast.LENGTH_SHORT).show();

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}
});

There's an answer to detect if user tries to put a number higher than n

Your question is quite difficult to understand, but yes, finally I got you, you lucky that I did something similar a year ago...

You first create this inner class called Max30TextWatcher (or whatever name)

public class Max30TextWatcher implements TextWatcher {

private EditText et;

Max30TextWatcher(EditText et) {
this.et = et;
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable editable) {
if(TextUtils.isEmpty(editable.toString())){
return;
}
try {
if (Integer.valueOf(et.getText().toString()) > 30) {
Toast.makeText(MainActivity.this, "Max is 30 dawg", Toast.LENGTH_SHORT).show();
//put all the TextView ""
et.setText("");

}
}
catch(NumberFormatException e){
//no dots and comma accepted
et.setText("");
}
}
}

So you change the .xml to this :

<EditText
android:id="@+id/someId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="2" />

And you call the new class as follows :

et = (EditText)findViewById(R.id.someId);
et.addTextChangedListener(new Max30TextWatcher(et));

And you are done, you cannot put a number higher than 30 :)

Happy coding!

Set max length for EditText

public void setEditTextMaxLength(EditText editText, int length) {
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(length);
editText.setFilters(FilterArray);
}

Android:: Set max-length of EditText programmatically with other InputFilter

Just try this way

InputFilter

InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; ++i)
{
if (!Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890]*").matcher(String.valueOf(source.charAt(i))).matches())
{
return "";
}
}

return null;
}
};

How to apply

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edt =(EditText)findViewById(R.id.edt) ;

edt.setFilters(new InputFilter[]{filter,new InputFilter.LengthFilter(10)});


}

Android EditText Max Length

Possible duplicate of Limit text length of EditText in Android

Use android:maxLength="140"

That should work. :)

Hope that helps

Android limit length to width of the EditText

Try this:

  editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


}

@Override
public void afterTextChanged(Editable editable) {
Rect bounds = new Rect();
Paint textPaint = editText.getPaint();
textPaint.getTextBounds(editText.getText().toString(), 0, editText.getText().toString().length(), bounds);
int width = bounds.width();
if (width > max_width) {
editText.setText(editText.getText().toString().substring(0, editText.getText().toString().length() - 1));
}

}
});

Limit text length in edit text

You should be able to put in your .xml file under your edit text:

android:maxLength="300"

You dont need any of that other stuff in your .java file. Your .xml file controls things like color, length, size, etc.



Related Topics



Leave a reply



Submit