2

I know that I can set the maximum length of an input text within an EditText by using

android:maxLength="10".

But can I also set the maximum length so that once the entire EditText is filled with text, the user can't type in anything anymore?

Something like this:

|_________________________|   <-- empty EditText   

|texttexttexttext_________|   <-- user can still type in text

|texttexttexttexttexttextt|  <-- that's it, user can't type in stuff anymore

How could I accomplish that?

EDIT: In other words: I don't want it limited to exactly 10 characters, but I want to limit it so that once the entire EditText is filled (which depends on the screen size), the user can't type in stuff anymore.

deimos1988
  • 5,896
  • 7
  • 41
  • 57
  • possible duplicate of [Limit text length of EditText in Android](http://stackoverflow.com/questions/3285412/limit-text-length-of-edittext-in-android) – Gabriel Negut Aug 01 '14 at 09:30
  • If i understand you correctly, you want to avoid the user to change the text when he typed 10 chars. Right? – Marco Acierno Aug 01 '14 at 09:31
  • @GabrielNegut No it's not, I don't want it limited to exactly 10 characters, but I want to limit it so that once the entire EditText is filled (which depends on the screen size), the user can't type in stuff anymore. – deimos1988 Aug 01 '14 at 09:33
  • @MarcoAcierno That's not what I want, please see my edited post. – deimos1988 Aug 01 '14 at 09:35

4 Answers4

7

java code

  EditText edittext;

  edittext.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});

XML

<EditText
android:maxLength="10"
/>
Sreelal S
  • 143
  • 2
  • 7
0

Well, that's what i think could be a way to do it:

  1. Get the EditText width
  2. Get the text width

Before anything, the code here is not perfect and it allows the user to put a bit more than the width (maybe due to the fact that EditText.getWidth() will return the value with borders etc.) It's just to give an idea.

More problems about this approch is the fact that even if we don't show the text inside the EditText the keyboard buffer will still know about that and it will create a bad effect for the user, i tried to use InputFilter but the result is the same (well, it was more buggy).

private int widthEditText;

// [...] in onCreate:

final EditText editText = (EditText) findViewById(R.id.text);
// p.s in onCreate the layout is not created yet, so i should wait to read the width
editText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        widthEditText = editText.getWidth();
    }
});

editText.addTextChangedListener(new TextWatcher() {
    private String previousText;

    @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) {
        Paint paint = editText.getPaint();
        Rect rectangle = new Rect();

        /* i used getTextBounds instead of measureText because it will return the float value but getWidth of
         * edit text is a int. */
        paint.getTextBounds(editable.toString(), 0, editable.length(), rectangle);
        Log.d(TAG, "Text width: " + rectangle.width());

        if (rectangle.width() > widthEditText) {
            /* the user cannot type anymore */
            Log.d(TAG, widthEditText + " > " + rectangle.width() + " blocked.");
            editable.replace(0, editable.length(), previousText);
            return;
        }

        previousText = editable.toString();
        Log.d(TAG, "Continue to write, new previousText: "  + previousText);
    }
});

Just to see, i tried to use measureText which returns the width in float (and work with comparing int and float) with the same result:

@Override
public void afterTextChanged(Editable editable) {
    Paint paint = editText.getPaint();
//                Rect rectangle = new Rect();

    /* i used getTextBounds instead of measureText because it will return the float value but getWidth of
     * edit text is a int. */
//                paint.getTextBounds(editable.toString(), 0, editable.length(), rectangle);
    float width = paint.measureText(editable.toString());

    Log.d(TAG, "Text width: " + width);

    if (width > widthEditText) {
        /* the user cannot type anymore */
        Log.d(TAG, widthEditText + " > " + width + " blocked.");
        editable.replace(0, editable.length(), previousText);
        return;
    }

    previousText = editable.toString();
    Log.d(TAG, "Continue to write, new previousText: "  + previousText);
}

The result is pretty the same...

I played around with android:ems="4" which should provide the font size (maybe something could be done to make it fit better?) but nothing (well not, seems like something it looks better but i feel it totally random based on the words).

Personally, i would avoid this since for me it sounds a bit random... maybe exists a better approch which i didn't know..

i tested it with a Samsung S4 device, so i don't know if it works with other devices! Sorry about that, but Genymotion don't works

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
0

use this

yourEditText.addTextChangedListener(new TextWatcher(){

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

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

            if(yourEditText.getText().toString().length()>=10){
                yourEditText.setEnable(false)
            }


        }

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

        }
jaimin
  • 563
  • 8
  • 25
-1

You can use android:singleLine=true

Semyon Danilov
  • 1,753
  • 1
  • 17
  • 37