7

I have a edit text and a text view and I want to set a Max length in my edit text and it show in my text view and every time a user input a characters it will minus the number of character. For example I set the Max length of my edit text to 150 and if the user input 150 characters he/she cannot input any-more.

How to fix this issue?

Neha Agarwal
  • 622
  • 7
  • 24
Jeremiah Me
  • 153
  • 1
  • 3
  • 11

6 Answers6

18

To set the max length of the EditText (pick one or the other):

  1. In your XML file (recommended), use the property android:maxLength="150" Ex:

    <EditText
        android:id="@+id/yourEditTextId"
        ...
        android:maxLength="150" />     
    
  2. Programmatically (in your onCreate method), like so:

    EditText et = (EditText)findViewById(R.id.yourEditTextId);
    et.setFilters(new InputFilter[] { 
        new InputFilter.LengthFilter(150) // 150 is max length
    });
    

To keep a counter of the length left in the EditText:

Add this listener in your onCreate method (or anywhere, but it makes sense in onCreate):

final EditText et = (EditText)findViewById(R.id.yourEditTextId);
et.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        TextView tv = (TextView)findViewById(R.id.yourTextViewId);
        tv.setText(String.valueOf(150 - et.length()));
    }

    @Override
    public void onTextChanged(CharSequence s, int st, int b, int c) 
    { }
    @Override
    public void beforeTextChanged(CharSequence s, int st, int c, int a) 
    { }
});
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • every time I input it unfortunately stopped what's the problem?? – Jeremiah Me Feb 12 '14 at 05:08
  • Two things: 1. Post your LogCat errors and 2. Post your entire code. Go to http://pastie.org to do that and send me a link. This code works for me. – Michael Yaworski Feb 12 '14 at 05:12
  • @JeremiahMe There must be more than that in the error message. Either way, you're getting a `NullPointerException`, which means something has not been set a value. Send me the entire class. – Michael Yaworski Feb 12 '14 at 06:07
1

You can set the length by using

editText.setFilters( new InputFilter[] { new InputFilter.LengthFilter(YOUR_LENGTH) } );

or in xml

maxLength = "LENGTH"

Then you can get set a TextWatcher and you can get the String there so length also.

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
1

Set android:maxLength:"150" for EditText in the layout file.

And in the class file implement addTextChangedListener() method for the EditText

yourEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        TextView textView = (TextView)findViewById(R.id.yourTextViewId);
        textView.setText(String.valueOf(150 - s.toString().length()));
    }

    @Override
    public void onTextChanged(CharSequence s, int st, int b, int c) 
    { }
    @Override
    public void beforeTextChanged(CharSequence s, int st, int c, int a) 
    { }
}
Sujith Thankachan
  • 3,508
  • 2
  • 20
  • 25
0

I had saw a lot of good solutions, but I'd like to give a what I think as more complete and user-friendly solution, which include:

1, Limit length. 2, If input more, give a callback to trigger your toast. 3, Cursor can be at middle or tail. 4, User can input by paste a string. 5, Always discard overflow input and keep origin.

here: https://stackoverflow.com/a/46922794/2468360

you can use callback to show the left number that can input.

hyyou2010
  • 791
  • 11
  • 22
-1

use this android:maxLength="150"

        <EditText
         android:id="@+id/editText10"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:ems="10"
         android:maxLength="150"
          />
-1

In xml use this:

android:maxLength="Length_size" // size that you want
Rajendra arora
  • 2,186
  • 1
  • 16
  • 23