Android: adding validation to an EditText field?
I need to add Validation to the EditText field so that:
- only numbers can be entered -Simple answer cannot be given (i.e. cannot just press enter)
If any of the above events happen, I just want the application not to accept that record and allow the user to try to re-enter a valid record.
How can i do this?
Associated opcode:
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 window edittext:
<EditText
android:id="@+id/etEnterAnswerRandomTest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Answer..." >
</EditText>
source to share
For getting only numbers as input use this in your edittext
android:inputType="0123456789"
Internally 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();
}
}
});
source to share
This will help you. I created a textwatcher and a simple method inputValidation
that returns a boolean value whether the editable is empty or not. If edittext is empty 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 result looks like this.
source to share
This may / may not be the answer you are looking for, but it works for me.
-
Adding a
addTextChangedListener
TextWatcher to yourEditText
-
Adding
android:inputType="number"
to your EditText xml
hope this helps :)
source to share
To get only numbers use in XML
following:android:inputType="number"
And to get a non-empty input, do the following inside the onClickListner of the button:
if(editText.getText().toString.length()<1){
//do something saying nothing is entered. Display toast may be
}
Using code you can do
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
source to share