can someone please help making pairs game and using this if statement can someone tell me if theres a bracket or semi colon missing cant see whats missing (pic2.getTag() == beck) is underlined have 4 buttons want to pair or reset them the buttons start with set tag name (boots) this works but no good for non match
This works
if (pic2.getTag() == pic1.getTag()){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);}
THis doesnt
pic1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic1.setTag(beck);
pic1.setBackgroundResource(R.drawable.becks);
if (pic2.getTag() == beck) {
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);
}
}
});
The problem is in the compare operator ==. View tags are Objects (most probably Strings), so you need to compare them with equals.-
if (pic2.getTag().equals(pic1.getTag()))
and
if (pic2.getTag().equals(beck))
Related
Im trying to make an app that converts the value that the user inputs into the editText. But when I run the program it shuts down and won't show the input the user inputted or the input divided by 2. I tried putting btCalculate.setText("Hi" + convert); and that displayed hi and the user input but when I get rid of the string and just have convert it shuts down. Can someone help me or make sense of what I'm trying to do?
Here is my code:
btCalculate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
int convert = Integer.parseInt(editText.getText().toString());
if (btCopper1.isPressed());
btCopper2.isPressed();{
btCalculate.setText(convert);
}
if (btCopper1.isPressed());
btSilver2.isPressed();
btCalculate.setText(convert/2);
}
});
}
}
Is a good practice to code as simple as possible. It will be easier to found and fix issues.
Exemple:
if(expression == true) {
// do this instructions
}
In your code, start fixing the {}, making it easier to understand:
if (btCopper1.isPressed()) {
btCalculate.setText(convert);
}
Think about it, it seems the error is in your logic.
I have created a my KeyListener for my textbox
txtEmail.addKeyPressHandler(new KeyPressHandler() {
#Override
public void onKeyPress(KeyPressEvent event) {
if(event.getUnicodeCharCode() == 32 || event.getUnicodeCharCode() == 44) {
myFunction();
txtEmail.setText("");
txtEmail.setFocus(true);
}
}
});
myFunction() just computing some value, event.getUnicodeCharCode() == 32 is SPACE and event.getUnicodeCharCode() == 44 is COMMA. so when the user presses space or comma, it will go to my function.
the problem is after the function the textbox should be empty, but it is not, if the user presses space to go to the function, after it, the textbox will contain space in the beginning, and comma if the user presses comma last..
sorry for my bad English, but i hope somebody understand my problem, thank you very much, any help will be greatly appreciated.
Read this manual for KeyEvents
I think you should use the keyReleased-Method, because the method will than be called after the actual event.
In the moment your method will be called before the KeyEvent is done. Thats why you get a space or a comma in the textbox.
I am making a currency converter with two spinners. I want to make an "if" function using the values of the spinner's selected item like below.
#Override
public void onClick(View v) {
if (spinner1.getSelectedItem()=="Dollars" && spinner2.getSelectedItem()=="Euros") {
convertDollarstoEuros();
}
if (spinner1.getSelectedItem()=="Euros" && spinner2.getSelectedItem()=="Euros") {
convertEurostoEuros();
}
Toast.makeText(MainActivity.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
The problem is that the toast is showing, but the currencies aren't converting. The toast part is working, but the spinner part isn't. Any help would be greatly appreciated. Here is my LogCat:
Try this :
if (spinner1.getSelectedItem().toString().equals("Dollars") && spinner2.getSelectedItem().toString().equals("Euros")
...
getSelectedItem() returns an Object . info . So you have to get the corresponding string first.
Then java compares strings using equals().
if (spinner1.getSelectedItem()=="Dollars" && spinner2.getSelectedItem()=="Euros") {
You can't compare Strings like that. You have to use the equals() method to compare them. Use this:
if (spinner1.getSelectedItem().toString().equals("Dollars") && spinner2.getSelectedItem().toString().equals("Euros")) {}
Check whether you called the id correctly or not? When I was working when improper id was called this exception used to arise. For eg In TextView, findViewById(R.id.textView1) but in xml file we may have set it to textView2.
I am trying to make a quiz app, so there are 5 radio buttons with possible answers, and only 1 is correct. Then there's a submit button, which has an onClick="clickMethod" to process the submission.
My clickMethod looks like this:
public void clickMethod(View v){
RadioGroup group1 = (RadioGroup) findViewById(R.id.radioGroup1);
int selected = group1.getCheckedRadioButtonId();
RadioButton button1 = (RadioButton) findViewById(selected);
if (button1.getText()=="Right Answer")
Toast.makeText(this,"Correct!",Toast.LENGTH_SHORT).show();
else
Toast.makeText(this,"Incorrect.",Toast.LENGTH_SHORT).show();
}
However I can't get the IF statement to work no matter what. If I try to make the toast with
"button1.getText()" as parameter it does print the "Right Answer" string, but for some reason inside the IF statement it's not working, and the ELSE is always executed even when I check the right answer.
Does anyone know what might be happening or a better way to do this?
You're not comparing strings correctly.
The == operator is used when we have to compare the String object
references. If two String variables point to the same object in
memory, the comparison returns true. Otherwise, the comparison returns
false. Note that the ‘==’ operator does not compare the content of the
text present in the String objects. It only compares the references
the 2 Strings are pointing to.
Read here: http://www.javabeginner.com/learn-java/java-string-comparison
You should use the equals String method for String comparison:
public void clickMethod(View v){
RadioGroup group1 = (RadioGroup) findViewById(R.id.radioGroup1);
int selected = group1.getCheckedRadioButtonId();
RadioButton button1 = (RadioButton) findViewById(selected);
if ("Right Answer".equals(button1.getText())) {
Toast.makeText(this,"Correct!",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,"Incorrect.",Toast.LENGTH_SHORT).show();
}
}
In Java you can't compare Strings with == you have to use equals():
if (button1.getText().equals("Right Answer"))
If you want to compare objects in Java you have to use equals() method and not the == operator
..
if (button1.getText().toString().equals("Right Answer")) {
Toast.makeText(this,"Correct!",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,"Incorrect.",Toast.LENGTH_SHORT).show();
}
I have a method that checks for a null value from an editText on a click of a button like so:
public void myClickHandler09(View chv){
if (text9.equals("")){
text9.setText("0");
}else{
converter(text9);
}}
The
converter(text9);
method is as shown:
public void converter(View view){
switch (view.getId()) {
case R.id.Button09:
RadioButton RadioButtons = (RadioButton) findViewById (R.id.RadioButton901);
float inputValue = Float.parseFloat(text9.getText().toString());
if (RadioButtons.isChecked()) {
text9.setText(String
.valueOf(convertRadioButtons(inputValue)));
}
break;
}}
private double convertRadiobuttons(float inputValue){
return inputValue * 6.4516;
}
The method is larger but here i've only called one radiobutton to shorten it.
Right now though the if statement seems to do absolutely nothing and so non of the rest of the code works. If i remove the method and rename
converter(View view){
to
myClickHandler09(View view){
then the code works and until you enter a null value into the EditText (then it crashes)
What am I doing wrong exactly here?
NOTE: the method name "myClickHandler09" is linked to the button as android:onClick in the xml
You need to do if("".equals(text9.getText().toString())) { ...
The toString() is there because the TextView will return a CharSequence which may or may not be a String.
Right now you are comparing the TextView itself to "", and not the String it is showing.
Edit - As far as the crash goes, you also want to catch the NumberFormatException that Float.parseFloat() throws.
float inputValue = 1.0f; // some default value, in case the user input is bad.
try {
inputValue = Float.parseFloat(text9.getText().toString());
} catch (NumberFormatException e) {
// possibly display a red flag next to the field
}
Why not try
if ("".equals(text9.getText())) {
} else {
}
You essentially have to do a getText() from a TextView and not equals a String with a TextView.
One thing I don't understand with your code is that you call:
converter(text9);
passing in the EditText, but by replacing converter(View view) with the function name myClickHandler09 (like so):
myClickHandler09(View view) {
the button being pressed with call this function (if you defined it in the xml layout onClick paramter).
So to match this behaviour with your current code, try this out:
public void myClickHandler09(View btnView){
if (text9.equals("")){
text9.setText("0");
} else {
converter(btnView);
}
}
I may have missed the point of you're post, but I think that is part of your issue. Also in stead of .equals("") I prefer (text9.toString().length() > 0) just seems a bit more logical, but that's me being a bit pedantic.