Android: Adding validation to EditText box?

12,506

Solution 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();
      }
    }

 });

Solution 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

Solution 3

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 :)

Share:
12,506
codeme2020
Author by

codeme2020

Updated on June 04, 2022

Comments

  • codeme2020
    codeme2020 almost 2 years

    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
    codeme2020 almost 10 years
    Thanks, is there a way to ensure that if the editText field is empty then the "submit" button can not be clicked?
  • Nabin
    Nabin almost 10 years
    Huh? Don't do anything then. i.e. in side onclicklistener check if empty. If empty don't do anything else do something that you want
  • codeme2020
    codeme2020 almost 10 years
    Thanks i think i get you! :)