Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the text in numbers only from 0-59 in Edit Text in Android?

I have an Edit Text in which the number can only between 0-59, No other numbers will be typed in that Edit Text, I tried text-watcher but did not get success. Any idea?

12-26 14:59:39.715: E/AndroidRuntime(19494): FATAL EXCEPTION: main
12-26 14:59:39.715: E/AndroidRuntime(19494): java.lang.StackOverflowError
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findViewShouldExist(View.java:4281)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.findUserSetNextFocus(View.java:4263)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.FocusFinder.findNextFocus(FocusFinder.java:66)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:569)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.View.focusSearch(View.java:4221)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.onCreateInputConnection(TextView.java:5378)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:977)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:932)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2958)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494):    at android.widget.TextView.setText(TextView.j
like image 289
sumit acharya Avatar asked Dec 26 '12 06:12

sumit acharya


People also ask

How do I cut text on android?

Android: How to Cut, Copy, and Paste TextTap and hold your finger on the text box you are copying text from for about 2 seconds until a menu appears at the top of the screen and the text becomes highlighted. Drag the selection to highlight the exact text you wish to work with. Select the “Cut” or “Copy” icon option.

Is there a way to define a min and max value for EditText in android?

Testing the min value is a different story as we cannot be sure if the user has finished typing or not and therefore cannot decide if we should block or not. Then in Activity set the InputFilterMax and the OnFocusChangeListenerMin to EditText note : You can 2 both min and max in onFocusChangeListener .


2 Answers

void addAddTextChangeListener(final EditText et) {
        et.addTextChangedListener(new TextWatcher() {
            boolean textModified = false;
            @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) {
                String strEnteredVal = et.getText().toString();
                if(!strEnteredVal.equals("") && !(strEnteredVal.length() < 2)) {
                    int num = Integer.parseInt(strEnteredVal);
                    if (!textModified) {
                        if (num > 60) {
                            et.setText("");
                        } else {
                            textModified = true;
                            et.setText("" + num);
                            et.setEnabled(false);
                        }
                    }
                }
            }

        });
    }

and android:inputType="number" in EditText entry in xml-file. But this code stops working once it gets correct input, so You should deactivate EditText after that.

like image 163
goaud27 Avatar answered Sep 20 '22 12:09

goaud27


Here's how I like to do this:

public class InputFilterMinMax implements InputFilter {
    private int min;
    private int max;

    public InputFilterMinMax(int min, int max) {
        this.min = min;
        this.max = max;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        //noinspection EmptyCatchBlock
        try {
            int input = Integer.parseInt(dest.subSequence(0, dstart).toString() + source + dest.subSequence(dend, dest.length()));
            if (isInRange(min, max, input))
                return null;
        } catch (NumberFormatException nfe) { }
        return "";
    }

    private boolean isInRange(int a, int b, int c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }
}

Then set the filter on your EditText:

myEditText.setFilters(new InputFilter[]{new InputFilterMinMax(0, 59)});
like image 27
user167019 Avatar answered Sep 18 '22 12:09

user167019