Limit an Edittext from 1 to 100 and include the "%" symbol

Asked

Viewed 352 times

1

I’m making an application that takes the data entered in a EditText and makes a certain calculation.

The problem is that I need the field to go from 1 to 100%, but the "Mask" I’m using is too simple for that, so it only does the limitation but it goes from 0 to 999 and that’s not what I want.

Nor can I place the % symbol at the end of that field.

Follows the code:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

import com.github.rtoshiro.util.format.MaskFormatter;
import com.github.rtoshiro.util.format.SimpleMaskFormatter;
import com.github.rtoshiro.util.format.pattern.MaskPattern;
import com.github.rtoshiro.util.format.text.MaskTextWatcher;

public class ActivityForm extends AppCompatActivity {


private EditText percentual;

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

    percentual = (EditText) findViewById(R.id.percentual_Id);
    SimpleMaskFormatter simpleMaskPercentual = new SimpleMaskFormatter( " NNN% " );
    MaskTextWatcher maskPercentual = new MaskTextWatcher(percentual, simpleMaskPercentual);
    percentual.addTextChangedListener( maskPercentual );
    }
}
  • 1

    You want it to go from 1% to 100% or from 0% to 100%?

  • from 1% to 100% because the customer must specify how much he wants to invest

  • 1

    Okay, I edited my answer. Tell me if it fits.

  • I’m new to this part of masks and I’m a little confused here... I downloaded the dependency directly from Github so I just copied and pasted in build.gradle... this code you posted, should be created in a new file?

  • 1

    Two files, one PercentTextWatcher.java and a ActivityForm.java. Both in the same folder where you already have the ActivityForm.java. If you can’t do it, let me know that I can easily put it in the same file.

  • Would it be too much to ask you to do that? I tried to create the file here and returned it: Unable to parse template "Class" Error message: This template Did not Produce a Java class or an interface

  • Okay, I edited the answer.

Show 3 more comments

1 answer

1

Analyzing the Maskformatter code on Github, I think the easiest approach is to implement the TextWatcher directly. The MaskFormatter doesn’t seem to have been made for a case like the one you have.

I will use a regular expression based approach. The idea is that it validates the input according to a regular expression. If the input is not valid, discard the last character and try again until the resulting string is valid or until it becomes empty. That’s what the method format(String) below does.

The substitute I think would be:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import java.util.regex.Pattern;

public class ActivityForm extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_form);
        EditText percentual = (EditText) findViewById(R.id.percentual_Id);
        PercentTextWatcher mask = new PercentTextWatcher(percentual);
        percentual.addTextChangedListener(mask);
    }

    private static class PercentTextWatcher implements TextWatcher {

        private static final Pattern PERC_PAT =
                Pattern.compile("(?:[1-9]|[1-9][0-9]|100)%?");

        private final TextView textView;

        private String currentText;

        public PercentTextWatcher(TextView textView) {
            this.regex = regex;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!charSequence.toString().equals(currentText)) {
                currentText = formatter.format(charSequence.toString());
                textView.setText(currentText);
                if (textView instanceof EditText) {
                    EditText editText = (EditText) textView;
                    editText.setSelection(currentText.length());
                }
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }

        private static String format(String in) {
            if (in == null) return "";
            for (String s = in; !s.isEmpty(); s = s.substring(0, s.length() - 1)) {
                if (PERC_PAT.matcher(s).matches()) return s;
            }
            return "";
        }
    }
}

See here a method test format(String) of the above code.

Note that there are two classes in this file. The class PercentTextWatcher is used to replace the MaskTextWatcher.

If you prefer to use 0% to 100% instead of 1% to 100%, just change the regular expression to "(?:[0-9]|[1-9][0-9]|100)%?" instead of "(?:[1-9]|[1-9][0-9]|100)%?".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.