-3

I am creating an app that calculates the the users marks. How do I go forward, I'm trying to limit the user's input value to between min=0 and max=100?

I've got 3 inputs that I want to limit.

 EditText a = (EditText) findViewById(R.id.Assignment1);
 EditText b = (EditText) findViewById(R.id.Assignment2);
 EditText c = (EditText) findViewById(R.id.Assignment3);

Tried using the following but only works when I execute my function.

public void YearMark(View v)
    {
        EditText a = (EditText) findViewById(R.id.Assignment1);
        a.setFilters(new InputFilter[]{ new InputFilterMinMax("0", "100")});
LinnoX
  • 27
  • 4
  • Thank you. I still have an issue. This only works when I execute my function. I tried declaring this outside the function but it does not work. – LinnoX Apr 01 '22 at 18:52

3 Answers3

0

As @JAY_Panchal mentioned, you can use an InputFilter:

public class RangeInputFilter implements InputFilter {
    private final int min, max;

    public RangeInputFilter(int min, int max) {
        boolean rightOrder = min <= max;
        this.min = rightOrder ? min : max;
        this.max = rightOrder ? max : min;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dStart, int dEnd) {
        try {
            String sourceStr = source.toString();
            String destStr = dest.toString();
            String result = destStr.substring(0, dStart) + sourceStr.substring(start, end) + destStr.substring(dEnd);
            int input = Integer.parseInt(result);
            if (min <= input && input <= max) return null;
        } catch (NumberFormatException ignored) { }
        return "";
    }
}

When a user changes the content of an EditText, you can use an InputFilter to decide what actually changes in the EditText.

Returning null means that the input is accepted.

Returning something else (in this case "") means that source should be replaced with that (in this case, we want dest to stay as it is and thus have an empty source).


To assign your filter:

RangeInputFilter filter = new RangeInputFilter(0, 100);
InputFilter[] filters = new InputFilter[] {filter};
editText1.setFilters(filters);
editText2.setFilters(filters);
editText3.setFilters(filters);
Cactusroot
  • 1,019
  • 3
  • 16
  • Thank you. I still have an issue. I've updated my question. What happens is that I have a function for a button and now this only works when I execute the button. – LinnoX Apr 01 '22 at 19:02
  • @LinnoX You mean the filter only works then? Sure - you are firstly setting the filter there, so it cannot work before. If you want it to be set at the start of your Activity, override the method onCreate() and apply the filter in there. – Cactusroot Apr 01 '22 at 19:08
0

Add this textwatcher for all three edittext a,b and c:

a.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                String strTest = editText1235.getText().toString().trim();
                if (!strTest.equals("")) {
                    float no = Float.parseFloat(strTest);
                    if (no > 100) {
                        a.setText(null);
                        Toast.makeText(this, "enter a value less than 100", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

Also set input type of these text fields as number so that user cannot enter a negative sign,ie, any value less than 0.

android:inputType="number"

While submitting the details also check if the field is not empty like below:

if (a.getText().toString().trim().length() <=0 ||b.getText().toString().trim().length() <=0 ||c.getText().toString().trim().length() <= 0){
Toast.makeText(this, "Value cannot be empty", Toast.LENGTH_SHORT).show();
}
Sneha
  • 11
  • 2
0

I don't know your usecase, but you might also want to use a SeekBar.

A SeekBar is a slider - you can select a number by sliding. It is also very customizable.

The main advantage is that the user does not need to open the keyboard.

Cactusroot
  • 1,019
  • 3
  • 16