0

i m getting an input using an EditText from the user but i don't want input more than 30 that is his input should be from 1-30 range, how should i do it??

        final EditText personcount = findViewById(R.id.personcount);
        final TextView personcountview = findViewById(R.id.personcountview);
        final TextView itemcountview = findViewById(R.id.itemcountview);
        final EditText itemcount = findViewById(R.id.itemcount);
        personcount.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
        itemcount.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
        Toast.makeText(this, "EditText limit set to a single digit", Toast.LENGTH_SHORT).show();
        Button next1 = findViewById(R.id.next1);
        next1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num1= Integer.parseInt(personcount.getText().toString());
                int num2 = Integer.parseInt(itemcount.getText().toString());

                Intent personname = new Intent(getApplicationContext(), personname.class);

                    personname.putExtra("num", num1);
                    personname.putExtra("num2",num2);
                    startActivity(personname);

            }
Farzad Kamali
  • 751
  • 2
  • 6
  • 24
harini t
  • 11
  • 1
  • Why are you using a `LengthFilter`? Do you want to restrict the length of the input to 1 character? In case you want to apply a min and max value for numeric input, see [this question](https://stackoverflow.com/questions/14212518/is-there-a-way-to-define-a-min-and-max-value-for-edittext-in-android). – deHaar Jun 12 '20 at 11:54

1 Answers1

0

you can set maxLength in your editText in xml :

android:maxLength="30"

or set filter :

TextView edit= new TextView(...);
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(30);
edit.setFilters(filterArray);
Farzad Kamali
  • 751
  • 2
  • 6
  • 24