I'm trying to get values out of edittext boxes and on the click of a button, I want to compare if the text inside the edittext box matches a certain value. If it does, a new intent should be called. If it doesn't it should show an error. Here is my code after the button is clicked...
if(v.getId() == R.id.button1){
id = et1.getText().toString();
Toast toast = Toast.makeText(this, "Value of id is: " + id, Toast.LENGTH_SHORT);
toast.show();
if(id == "abc"){
Intent i= new Intent(Slogin.this, Sales.class);
startActivity(i);
}else{
Toast toast = Toast.makeText(this, "Wrong id pass", Toast.LENGTH_SHORT);
toast.show();
}
Now the problem is that even when I enter "abc" (without the double commas), it still shows me the error "Wrong id pass". While the Toast clearly shows that I have entered abd and the string id now holds the value "abc". Help required...
Try this.. if id is int use like this id == 5. but id is String you should use id.equals("abc")
if(id.equals("abc")){ //Correction is here
Intent i= new Intent(Slogin.this, Sales.class);
startActivity(i);
}else{
Toast toast = Toast.makeText(this, "Wrong id pass", Toast.LENGTH_SHORT);
toast.show();
}
== always just compares two references (for non-primitives, that is) - i.e. it tests whether the two operands refer to the same object.
However, the equals method can be overridden - so two distinct objects can still be equal......
Use
if(id.equals("abc"))
instead Of
if(id == "abc")
Related
I want to display the value toast in text view
Code :
public void onAnimationEnd(Animation animation) {
#SuppressLint({"WrongConstant", "ShowToast"}) Toast toast = Toast.makeText(this, " " + String.valueOf((int)(((double)this.intNumber)
- Math.floor(((double)this.lngDegrees) / (360.0d / ((double)this.intNumber))))) + " ",0);
toast.setGravity(49,0,0);
this.blnButtonRotation = true;
b_start.setVisibility(View.VISIBLE);
}
i agree with Laura. It's hard to guess what you are asking for here.
If you want to show the Toast you should add .show() at the end of your toast and change the duration to Toast.LENGHT_SHORT or Toast.LENGTH_LONG instead of "0".
e.g:
Toast.makeText(this, "yourString", Toast.LENGTH_LONG).show()
If you want to access the the value of the String inside the Toast, there is currently no way to do something like that. So the only option is to store it in its own variable:
String numberAsString = String.valueOf((int(((double)this.intNumber) - Math.floor(((double)this.lngDegrees) / (360.0d / ((double)this.intNumber)))));
textView.setText(numberAsString);
//Toast toast = Toast.makeText(this, numberAsString, Toast.LENGHT_LONG).show();
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();
}
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!!!
I have some if statements, I do not know how to show the result on the screen.
Below are 2 things I have tried. I know the system.out goes to the log.
if (Enter == "1") {
// tv.setText("This is the display 1");
System.out.println("The 1");
}
else if (Enter == "2") {
System.out.println("The 2");
}
What is Enter? If it is an instance of an object, use lowercase names, so that would be enter.
To answer the question, you're probably comparing Strings. You should use .equals instead of ==.
So:
String enter = "1"; //your variable
if(enter.equals("1")){
System.out.println("The 1");
}else if(enter.equals("2"){
System.out.println("The 2");
}
When comparing primitive data types (like int, char, boolean) you can use ==, !=, etc.
When comparing objects (like String, Car, etc) you need to use the .equals() method.
See also this page.
Edit
Use a Toast:
Toast.makeText(this, "The 1", Toast.LENGTH_SHORT).show();
See here.
Do it with a Toast instead.
A Toast is a popup-like element in Android displaying a short message for a predefined duration on screen.
String enter = "whatever value enter has";
int duration = Toast.LENGTH_SHORT;
Context context = getApplicationContext();
String message = "The Nothing";
if (enter.equals("1")) {
message = "The 1";
} else if (enter.equals("2")){
message = "The 2";
}
Toast messageToast = Toast.makeText(context, message, duration);
messageToast.show();
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();
}