3

I need to add Validation to my EditText box so that:

-Only numbers can be entered -An empty answer cannot be given (i.e. cannot just press enter button)

If any of the above do happen, I just want the application to not accept the entry and allow the user to attempt to re-enter a valid entry.

How can I do so?

Related activity code:

 answer = (EditText) findViewById(R.id.etEnterAnswerRandomTest);

     //when submit button is clicked
       public void onClick(View v) {

         switch(v.getId()) {

         case R.id.btnSubmitRandomTest:
            // sets string equal to what is entered in editText
            final String entry = answer.getText().toString();
            // convert from string value to int
            int a = Integer.parseInt(entry); 

XML of edittext box:

<EditText
        android:id="@+id/etEnterAnswerRandomTest"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Answer..." >
    </EditText>
codeme2020
  • 853
  • 2
  • 13
  • 20

4 Answers4

2

This should help you out. I've created a textwatcher and the simple method inputValidation which returns a boolean whether the editable is empty or not. If the edittext is blank when the validation label is displayed.

TextView input_validation;
EditText input;
int final_input_value;

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

public void setupViews() {
    input_validation = (TextView) findViewById(R.id.input_validation);
    input_validation.setVisibility(View.GONE);
    input = (EditText) findViewById(R.id.input);
    input.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }
        @Override
        public void afterTextChanged(Editable editable) {
            if (inputValidation(editable)) {
                input_validation.setVisibility(View.VISIBLE);
                input_validation.setText("You must enter your Age");
            } else {
                input_validation.setVisibility(View.GONE);
                final_input_value = Integer.parseInt(editable.toString());
            }
        }
    });
}
public boolean inputValidation(Editable inputText) {
    return inputText.toString().isEmpty();
}

Layout

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/input_label"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Your Age"
    android:textStyle="bold"
    android:textColor="@android:color/holo_blue_dark"
    android:paddingBottom="16dp" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/input"
    android:inputType="number"
    android:layout_below="@+id/input_label"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/input_validation"
    android:textStyle="italic"
    android:textColor="@android:color/holo_red_dark"
    android:layout_below="@+id/input"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

The output looks like this.

enter image description here

Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79
1

This could / could be not the answer your looking for, but it works for me.

  1. Adding addTextChangedListener TextWatcher to your EditText

  2. Adding android:inputType="number" to your EditText xml

How to use TextWatcher

hope it helps :)

Community
  • 1
  • 1
Spurdow
  • 2,025
  • 18
  • 22
1

For getting only numbers as a input use this in your edittext

android:inputType="0123456789"

Inside onCreate use this code

edittext.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(editText.getText().toString.length()<1){
        // Display toast 
        Toast.makeText(getApplicationContext(), "Please enter something !",Toast.LENGTH_LONG).show();
      }
    }

 });
Vivek Warde
  • 1,936
  • 8
  • 47
  • 77
0

For getting only number use the following in XML

android:inputType="number"

And to get non empty input do the following inside onClickListner of button:

if(editText.getText().toString.length()<1){
//do something saying nothing is entered. Display toast may be
}

Using code, you could do

editText.setInputType(InputType.TYPE_CLASS_NUMBER);
Nabin
  • 11,216
  • 8
  • 63
  • 98