0

I have an EditText that will put this sets of value

58.44,44.2 or even negative like -58.44,-44.2

how can I prevent it from surpassing between -100 and 100 i tried this link but no joy.

I wanna make my EditText to type between -100 and 100 only if surpasses then dont continue typing.

myown email
  • 139
  • 1
  • 10

3 Answers3

1

You can add the condition like this

editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() > 0) {
                String value = s.toString();
                Double numericValue = Double.parseDouble(value);
                if (numericValue < -100 || numericValue > 100) {
                    editText.setText("");
                }
            }
        }
    });

Set your EditText inputType as

android:inputType="numberDecimal|numberSigned"
Seeker
  • 1,030
  • 10
  • 10
  • tried typing but nothing shows and after i input a "-" an error occurs `java.lang.NumberFormatException: For input string: "-"` – myown email Dec 06 '18 at 12:55
  • Please handle NumberFormatException, it happens while calling parseDouble method. The remaining logic is if you enter any number > 100 or < -100 it would reset the EditText to blank – Seeker Dec 06 '18 at 13:00
  • i add a try and catch but nothing shows when i type – myown email Dec 06 '18 at 13:03
  • That is strange, the entered text should appear unless it is out the of the range. – Seeker Dec 06 '18 at 13:09
  • even i just used the condition then shows some message if it not falls between but after inputing the right value it always says it is not between 100 – myown email Dec 06 '18 at 13:16
  • The condition definitely works. It is just (-100 <= x <= 100). I am not sure why you cannot see entered text. I did not implement the code, but I am positive that it works. I will check and update in case there is any issue. – Seeker Dec 06 '18 at 15:30
  • Hi, I just tried my above answer. I can absolutely see entered text. I just edited the login removing = to accept 100 and -100 as well. If it is not working for, it would help if you share your code. – Seeker Dec 08 '18 at 12:27
0

If you want to solve programmatically, TextWatcher is a good option.

Inside onTextChanged(), Check if your changed CharSequence s, matches you need, otherwise replace it.

editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {


}

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

}

@Override
public void afterTextChanged(Editable s) {

}
});
touhid udoy
  • 4,005
  • 2
  • 18
  • 31
0

Using a regex pattern you can handle the numbers between -99.99 and 99.99

^[-+]?[0-9]{1,2}\.[0-9]{1,2}+$

Update 2: For >=-100 and <=100, use this regex:

^[-+]100|^[-+]?[0-9]{1,2}.[0-9]{1,2}+$

Below sample, checks the text and enables/disables the button if matches or not.

EditText mEditText;
Button mButton;
final static String mRegex = "^[-+]100|^[-+]?[0-9]{1,2}\.[0-9]{1,2}+$";
boolean inputMatches;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mEditText = findViewById(R.id.editText);
    mEditText.addTextChangedListener(new InputController());

    mButton = findViewById(R.id.button);
    mButton.setEnabled(inputMatches);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                Toast.makeText(MainActivity.this," Input matches!", Toast.LENGTH_SHORT).show();
        }
    });
}

public class InputController implements TextWatcher {
    @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) {
        String input = mEditText.getText().toString();
        inputMatches = input.matches(mRegex);
    }
}

Update:

As per your request on clearing Edittext.. There are multiple ways to do it. You can either follow other answers above or you can use my above answer if it fits with you and instead using TextWatches, you can use OnFocusChangeListener. This is better if you have multiple Edittexts. When your one Edittext loses focus, then this code will trigger to see if it matches the regex pattern. If no match, clears it. Remove above TextWatcher, follow bellow:

  1. Implement OnFocusChangeListener in your Activity

    MainActivity extends AppCompatActivity implements View.OnFocusChangeListener
    
  2. Override its onFocusChange method.

    @Override
     public void onFocusChange(View view, boolean b) {}
    
  3. Inside onFocusChange do your checks.

    @Override
    public void onFocusChange(View view, boolean b) {
        if(!b){ // Lost focus
            String text = ((EditText)view).getText().toString();
            if(text.matches(mRegex)){
                // It matches
                Toast.makeText(MainActivity.this, "Input matches!!!", Toast.LENGTH_SHORT).show();
            }
            else{
                // No match - clear Edittext
                ((EditText)view).setText("");
                 Toast.makeText(MainActivity.this, "Input doesn't match", Toast.LENGTH_SHORT).show();
            }
        }
    }
    

Update 2

To include -100 and 100, use this regex pattern:

^[-+]100|^[-+]?[0-9]{1,2}.[0-9]{1,2}+$

Akn
  • 421
  • 5
  • 9