validating EditText input - android

Trying to confirm that an EditText tool is not empty when the user clicks a button. Whenever the EditText is not blank the code works. But whenever it is blank my program crashes. Any ideas what I'm doing wrong? Thanks.
if (et1.getText().toString() != null) {
inpt_weight = Integer.parseInt(et1.getText().toString());
Toast.makeText(getBaseContext(), "weight is not null", Toast.LENGTH_LONG).show();
} else {
inpt_weight = 0;
Toast.makeText(getBaseContext(), "weight is null", Toast.LENGTH_LONG).show();
};

I think its trying to parse an empty string("") to an integer which is not possible
try:
if (!et1.getText().toString().equalsIgnoreCase("") && et1.getText().toString() != null)

This will be a good way:
if(et1.getText().toString().trim().length()==0){
Log.d("No data","No text found in the edit text");
}
This also checks if the user had put nothing but only spaces.
In your code, I see you're converting the text to numbers.
In this case, either put correct IMEMethod for the edit text so that it takes only numbers, or check in your code that user has entered only numeric characters, else you'll end up with a NumberFormatException while conversion if the user has entered non numeric characters.

Try the following code please..
if(et1.getText().toString().equals("")){
Toast.makeText(getApplication(), "weight is null", Toast.LENGTH_SHORT).show();}
else{
inpt_weight = Integer.parseInt(et1.getText().toString());
Toast.makeText(getApplication(), "weight is not null & it is : "+inpt_weight,Toast.LENGTH_SHORT).show();
}

Related

Android: changing variable using if statement

This is a OnClickListener for button
name is EditText
I want it print only "hi" if nothing is entered, but "hi" + name + "!" if user inputs his/her name.
public void onClick(View view) {
if (button==view) {
String message;
if (name.getText().toString().matches("")) {
message = "hi!";
return;
}
else {
message = "hi" + name.getText().toString() + "!";
return;
}
Toast toast = Toast.makeText(this,message,Toast.LENGTH_SHORT);
toast.show();
display.setText(message);
}
}
For some reason I got error "unreachable statement" for the line:
Toast toast =...,
And if I compile and run it, the screen will output on 2 lines instead of one, such as:
Hi
!
What did I do wrong in here?
Why do you have the return statement? You need to show the Toast and then return. Remove the return statements.

How do I recognize the initial white space/spaces in my edit text?

I am trying to recognize the initial spaces in my edit text, such that if a user enters " " (any number of spaces) ,it doesnt enable my done button.
So, far I have this code in placE:
String sendString = mSendText.getText().toString();
if(sendString.equals(" ")||sendString.isEmpty()||sendString ==null ){
//do nothing
}else {
//do my stuff
}
The thing is I want the else to work only when I have string with any characters in it as long as it is not JUST ALL whitespace in the beginning.
The code I have works for only 1 whitespace. I want to make it such that no matter how many number of whitespaces exist in the beginning it will remove them or not enable my done button as long as no characters show up.
For example :
This should go to the if loop: " "
This should go to the else loop: " Hello, it's me"
Any ideas?
Thanks!
Just replace equalsTo() to startsWith():
String sendString = mSendText.getText().toString();
if( (sendString == null) || (sendString.startsWith(" ")) || (sendString.isEmpty())){
//do nothing
}else{
//do my stuff
}
Perhaps, if you're interested only in relevant text, you can exclude white spaces in the beginning/ending just using trim()
String sendString = mSendText.getText().toString().trim();
if(sendString.isEmpty()) {
//do nothing
}else{
//do my stuff
}
Use trim() to delete odd spaces from begin and end of String.
String str = new String(" Welcome to Tutorialspoint.com ");
System.out.print("Return Value :" );
System.out.println(Str.trim() );
Returns Welcome to Tutorialspoint.com
I would remove all spaces then check to see if there is anything in the string after that.
String sendString = mSendText.getText().toString().replace(" ","");
if(sendString.isEmpty()){
// do nothing
}else{
// do something
}

Comparing two EditText values in android

I'm trying to make an app where the user enters a word into an EditText box. Then, they enter something into another box and it checks to see if they are the same word. Here's the code that I used:
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
textfield2.setText(textfield2.getText().toString());
if(word == answer){
Toast.makeText(getApplicationContext(), "correct",
Toast.LENGTH_LONG).show();
}else
Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_LONG).show();
}
However, it always says that the two strings aren't the same even if they are. Is there a way to fix this?
You can't compare strings with the == operator.
Use .equals() instead:
if(word.equals(answer)) {
//do whatever
}
Use String.equalsIgnoreCase for comparing content of both string variables.:
if(word.equalsIgnoreCase(answer)){
}
Use:
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
if(answer.contentEquals(word)){
// Do something if equals
}
else{
// Do something if not equals
}
I think the best way to do this is using TextUtils:
if(TextUtils.equals(textfield1.getText(),textfield2.getText())){
//do something
}
instead of
if(word.contentEquals(answer)){
}
Use
if(word.equals(answer))
as we cant compare strings with Equal to (==) operator
Try This::
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
if(word.equals(answer)){
Toast.makeText(getApplicationContext(), "correct",
Toast.LENGTH_LONG).show();
}else
Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_LONG).show();
}

EditText Validation setError not working, getting crashed

I'm doing the check for my two EditText field editNumber1 and editNumber2.
I'm trying to display if the check perform fails.
I'm not able to display the error and the application crashes.
here is the sample code can any one suggest?
if ((editNumber1.getText().length() == 0) || (editNumber1.getText().toString() == " ") ||
(editNumber2.getText().length() == 0) || (editNumber2.getText().toString() == " "))
{
editNumber1.setFocusableInTouchMode(true);
editNumber1.requestFocus();
editNumber1.setError("Some inputs are empty");
}
Firs of all use this good utilities StringUtils.isNotBlank( string_to_compare );
this method return false if the string_to_compare is:
null,
empty (""),
blank (" ")
then try to use a break point in the lines below to see when the crash occurs
editNumber1.setFocusableInTouchMode(true);
editNumber1.requestFocus();
editNumber1.setError("Some inputs are empty");

Updating a default Phone Number

I have an EditText field, which I have a default value from
strings.xml ( android:text="#string/DefaultMobileNumber").
When the user updates this (button listener), I am storing the new value in SHARED_PREFERENCES, however, the new value will not show on screen when the page is re-displayed (the default from strings.xml persisits). I am using
final EditText phoneNoText = (EditText) findViewById(R.id.InPhone);
if (mSettings.contains(PREFERENCES_PHONENO)) {
String sPhoneNoText = (mSettings.getString(PREFERENCES_PHONENO,"No Number"));
phoneNoText.setText(sPhoneNoText);
//Toast.makeText(getBaseContext(), sPhoneNoText, Toast.LENGTH_SHORT).show();
}
else
{// write default value to PREFERENCES_PHONENO
editor.putString(PREFERENCES_PHONENO, "07799060000");
editor.commit();
//Toast.makeText(getBaseContext(), "No Phone", Toast.LENGTH_SHORT).show();
};
Hopefully I've made a stupid error, but can't seem to find it!!!

Categories

Resources