Alisaberi
Alisaberi

Reputation: 1

Set limitation on inserting edittext length in java code

I want to create an edittext that has a limitation on input length. When the edittext length is more than five it should display an error toast message. The app compiles but it crashes in the emulator and does not open. My code is below, any help would be appreciated.

<EditText
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:id="@+id/etext1"
  />



    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="B1"
    android:id="@+id/one"
    android:text="1"
    android:layout_below="@+id/etext1"
    android:gravity="center"

    />

The Java code looks like this:

public class MainActivity extends ActionBarActivity {

EditText tx=(EditText)findViewById(R.id.etext1);
String a;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }



     public void B1(View v) {

         if (tx.getText().toString().length()>5)

         {


             LayoutInflater inflater = getLayoutInflater();

             View erro = inflater.inflate(R.layout.error, (ViewGroup) findViewById(R.id.idoferror));


             Toast dis = Toast.makeText(this, "er", Toast.LENGTH_LONG);
             dis.setGravity(Gravity.CENTER | Gravity.RIGHT, 0, 0);
             dis.setView(erro);
             dis.show();


         }

         else {

             a=tx.getText().toString();
             a=a+1;
             tx.setText(a);




         }

     }

}

Upvotes: 0

Views: 43

Answers (2)

N J
N J

Reputation: 27505

move this

EditText tx=(EditText)findViewById(R.id.etext1);

line in onCreate() And set android:maxLength="5"

Edit

EditText tx;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tx=(EditText)findViewById(R.id.etext1);
  }

Upvotes: 1

Linh
Linh

Reputation: 60913

To show the Toast when you reach the limit characters. You need to add TextWatcher to your EditText.
After each time you enter the text to your EditText, check the length of your EditText then show the Toast and setText() again for your EditText if needed

final int MAX_CHARACTERS = 5;
...
yourEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
    }

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

    @Override
    public void afterTextChanged(Editable s) {

        if (yourEditText.toString().length() > MAX_CHARACTERS) {
            yourEditText.setText(yourEditText.getText().toString().substring(0, MAX_CHARACTERS));
            Toast.makeText(getActivity(), "Maximum number of characters reached.", Toast.LENGTH_SHORT).show();
        }

    }
});

===
IF you just only want to limit the text length not show the Toast. You can use

yourEditText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(MAX_CHARACTERS)});

or in XML

android:maxLength="5"

Hope this help

Upvotes: 0

Related Questions