user3614910
user3614910

Reputation: 1

User input shouldn't exceed a particular number entered in edittext in android

Am developing a hymn app in android, is there a way to let users know that the number they have entered cannot be found in the database, thus the hymn index they entered the hymn is not up to that number immediately the entered it in the edit text.

This is a section of the code

android:ems="10"
android:inputType="number"
android:maxLength="3"`

Upvotes: 0

Views: 64

Answers (2)

Margarita Litkevych
Margarita Litkevych

Reputation: 2136

Restricting length of the EditText could work if your value is inside [-99;999]. Anyway you should read and then validate the number.

  1. EditText.

    a. If you have a button (user enters hymn number and clicks a button to find), then add something like this in your onClick method:

    Editable e = yourEditText.getText();
    String hymnNumberInString = "";
    if (e != null) s = hymnNumberInString.toString();
    if (hymnNumber.isEmpty()) showEmptyAlert(); //show alert that string is empty;
    try {
        Integer hymnNumber = Integer.valueOf(s);
        if (!findHymn(hymnNumber)) {//here is a search
           showErrorMessage();
        }
    } catch (NumberFormatException e) {
        e.printStackTrace();
        showErrorMessage();
    }
    

    b. If you do not have a button, you can add a TextWatcher and show error if hymn number is exceeded:

    yourEditText.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) {
      if (s != null && s.length() != 0) {
        try {
           Integer hymnNumber = Integer.valueOf(s);
           if (findHymn(hymnNumber)) {
              //everything is ok, do what you want with it
               // BUT!!! Remember that user might entered only 1 and is still entering!
               // To ensure that user already entered (or maybe already entered) you can wait for 2 sec. 
    
               //E.g. by using someHandler.postDelayed(runnableWithThisCodeInside, 2000);    
           } else {
              showErrorMessage();
           }
        } catch (NumberFormatException e) {
           e.printStackTrace();
           showErrorMessage();
        }
      }
    }
    });
    

    c. You can use this nice library to simplify proccess of validation.

  2. For predefined set of numbers you can use NumberPicker. This component takes a String array as input (via setDisplayedValues()) - so you can populate it with numbers/string from the database. And its editable (unless you restrict it) - so your user can still enter the number he wants.

Upvotes: 1

Saurabh
Saurabh

Reputation: 1005

is there a way to let users know that the number they have entered cannot be found in the database.

Yes you can do that,Considering you are using EditText to let user enter the number, get the text from there like below

EditText mEdit   = (EditText)findViewById(R.id.edittext);
Integer number=Integer.valueOf(editText.getText.toString());

Now you have the number you can run a query on database table to match against the corresponding values, whether it exists in database or not.Something like this

    int count= SELECT count(*) FROM tbl_user
    WHERE name = ' + number + '

   if(count>0){
     Log.d("Count","Value exist in database");
   }

Upvotes: 0

Related Questions