0

I want to control EditText which only accepts int values. User enters numbers then I will add string (easy part). But I want to change it dynamically as user enters numbers. How can I achieve this?

I can do these:
user writes 61, EditText output is: 61 min.
user writes 1314, then EditText output is: 13 min 14 sec.

Here is the problem:
If user writes 7699, expected EditText output will be: 76 min 59 sec.

I think I need a controller thing like ArrayAdapter's getView() method. If user writes 1361 then I have to say to phone do not print "13 min 61 sec", just print "13 min 59 sec" in EditText. But I dont know how.

Solution I have tried: How to stop the edit text to add text,when the required format text is entered in it?

Any help will be appriciated.

Muhammed Aydogan
  • 100
  • 1
  • 11

2 Answers2

0

There is this function from DateUtils that you can use, you just need to give it a Long.

/**
     * Formats an elapsed time in the form "MM:SS" or "H:MM:SS"
     * for display on the call-in-progress screen.
     * @param elapsedSeconds the elapsed time in seconds.
     */
    public static String formatElapsedTime(long elapsedSeconds) {
        return formatElapsedTime(null, elapsedSeconds);
    }

to use it it's really easy, DateUtils.formatElapsedTime(time) and it'll print in String your time formatted by MM:SS

Biscuit
  • 3,364
  • 3
  • 15
  • 33
  • Thanks, but you didnt get what I asked. I want to correct the wrong input format dynamically, like: 65 second to 59 second because there cant be more than 59 seconds in a minute. – Muhammed Aydogan Mar 17 '20 at 22:42
  • what code are you using to display your formatted string ? You might wanna take a look at : https://stackoverflow.com/questions/625433/how-to-convert-milliseconds-to-x-mins-x-seconds-in-java – Biscuit Mar 17 '20 at 22:57
0

For allowing only numerical values, you can use android:inputType="number" in your EditText XML file

For tracing the value written in the EditText, you can listen to the changes of EditText using addTextChangedListener method, and TextWatcher interface

 Timer timer = new Timer();
    final long DELAY = 2000; // in ms
    field1 = (EditText)findViewById(R.id.field1);

    field1.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(final Editable s) {
            // Do whatever you want with the updated value
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // TODO: do what you need here
                    // you will probably need to use
                    // runOnUiThread(Runnable action) for some specific
                    // actions


                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            String minsString = "";
                            String secsString = "";
                            switch (s.length()) {
                                case 1:
                                    if (Integer.parseInt(String.valueOf(s)) == 0 || Integer.parseInt(String.valueOf(s)) == 1) {
                                        field1.setText(s + " min");
                                    } else {
                                        field1.setText(s + " mins");
                                    }
                                    break;
                                case 2:
                                    field1.setText(s + " mins");
                                    break;
                                case 3:
                                    minsString = s.toString().substring(0, 2);
                                    secsString = s.toString().substring(2);
                                    if (Integer.parseInt(secsString) > 59) {
                                        secsString = "59";
                                    }
                                    field1.setText(minsString + " mins " + secsString + " secs");
                                    break;
                                case 4:
                                    minsString = s.toString().substring(0, 2);
                                    secsString = s.toString().substring(2);
                                    if (Integer.parseInt(secsString) > 59) {
                                        secsString = "59";
                                    }
                                    field1.setText(minsString + " mins " + secsString + " secs");
                                    break;
                                default:
                                    break;
                            }
                        }
                    });

                }

            }, DELAY);

        }

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

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (timer != null)
                timer.cancel();
        }
    });

refering to timer solution to prevent the instance update of edit text

devMohaned
  • 61
  • 3
  • It is going to inner loop itself, never changes anything. Always calling itself again and again before changing text after running `field1.setText` method. I think we cannot write `setText` in `afterTextChanged` method – Muhammed Aydogan Mar 19 '20 at 11:58
  • I've added timer to the solution to make sure to wait 2secs after user finished typing, and then we'll convert the time to mins & secs. I've updated the code above. – devMohaned Mar 19 '20 at 15:10