3

I was new in Android and found some good tutorials on the internet, so I tried a simple activity with an if-else statement. I'm trying "correct and wrong" prompt/Toast:

Button page1 = (Button) findViewById(R.id.button2);
       page1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                final ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
                if (iv1.equals(R.drawable.airplane1)) {
                    Toast.makeText(getApplicationContext(), "Correct",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane2)) {
                    Toast.makeText(getApplicationContext(), "Please put an answer",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane3)){
                    Toast.makeText(getApplicationContext(), "Wrong",
                            Toast.LENGTH_SHORT).show();
                }
            }

       });

I'm not sure what is wrong in my if-else statement but never prompts at all. I tried removing the (iv1.equals(R.drawable.airplane3)) and (iv1.equals(R.drawable.airplane2)) then it only shows the wrong Toast. I can't get seem to make the correct to prompt me.

Here is the full code of my class:

public class MainActivity extends Activity {
private static final Random imagerandom = new Random();

private static final Integer[] Imagesnumber = 
    { R.drawable.airplane1, R.drawable.airplane2, R.drawable.airplane3, };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Integer a = Imagesnumber[imagerandom.nextInt(Imagesnumber.length)];
    final ImageView iv = (ImageView) findViewById(R.id.imageView1);

    View nextButton = findViewById(R.id.button1);
    nextButton.setOnClickListener(new Button.OnClickListener() {
         public void onClick(View V) {
              int resource = Imagesnumber[imagerandom.nextInt(Imagesnumber.length)];
              iv.setImageResource(resource);
         }
    });
    Button page1 = (Button) findViewById(R.id.button2);
       page1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                final ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
                if (iv1.equals(R.drawable.airplane1)) {
                    Toast.makeText(getApplicationContext(), "Correct",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane2)) {
                    Toast.makeText(getApplicationContext(), "Please put an answer",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane3)){
                    Toast.makeText(getApplicationContext(), "Wrong",
                            Toast.LENGTH_SHORT).show();
                }
            }

       });




}

}
Cat
  • 66,919
  • 24
  • 133
  • 141
NewbieontheSide
  • 37
  • 1
  • 3
  • 8
  • Let me see if I understand you correctly, are you trying to see what image is displayed in yout `iv1`? – Rotary Heart Feb 26 '13 at 18:40
  • yes i`m trying to see if the imaged displayed in iv1 is correct or wrong using the if else i`m not quite sure what is wrong in my if else statement – NewbieontheSide Feb 26 '13 at 18:42
  • you are missing the else condition.i mean the condition which doesnt satisfy the 3 of the ifs you specified – Lithu T.V Feb 26 '13 at 18:44
  • the issue is you are trying to see if the Image view is equal to the Id of a drawable these are things that will never be equal as the are different objects. you may want to look for a way to get the current drawable from the imageview and then see what that equals. – Eluvatar Feb 26 '13 at 18:45
  • You're comparing integers to ImageViews. That won't work. R.drawable.airplane1 is not an image itself, it's an int that servers as a resource identifier. – Charlie Collins Feb 26 '13 at 18:45

4 Answers4

2

R.drawable.airplane* is an int, which means you are comparing an ImageView object with an int, which is never true. Use iv.getDrawable().equals(getResources().getDrawable(R.drawable.airplane1)); to compare to Drawable objects.

This is probably not the best option performance wise, you may want to keep track of the index of the displayed image as a class variable, and do a conditional based on that (or something along those lines).

invertigo
  • 6,336
  • 5
  • 39
  • 64
  • Hello invertigo i`ve tried what you suggested with this "if (iv1.getDrawable()==getResources().getDrawable(R.drawable.airplane1)) { Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_SHORT).show(); }" i`m still getting the wrong toast – NewbieontheSide Feb 26 '13 at 18:53
2
iv1.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.airplane1).getConstantState())                                                                                                                                                                                                                                                                                                                                                                                                                                              
Zabador
  • 607
  • 3
  • 12
  • +1 for let us know....Is it always work like this ?..I have tried this and is working – Pragnani Feb 26 '13 at 19:25
  • one question on my mind again is that does this load memory for loading images? cause i`m afraid that i might get the error outofmemory error in logcat – NewbieontheSide Feb 26 '13 at 19:32
1

You can't compare type of ImageView with the type of int

iv1.equals(R.drawable.airplane1)

this is wrong

try this

iv.getDrawable()==getResources().getDrawable(R.drawable.airplane1)

instead

Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • Hello Pragnani i`ve tried it with this "if (iv.getDrawable()==getResources().getDrawable(R.drawable.airplane1)) { Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_SHORT).show(); } else if (iv.getDrawable()==getResources().getDrawable(R.drawable.airplane2)) { Toast.makeText(getApplicationContext(), "Please put an answer", Toast.LENGTH_SHORT).show(); } else if (iv.getDrawable()==getResources().getDrawable(R.drawable.airplane3)) { Toast.makeText(getApplicationContext(), "Wrong", Toast.LENGTH_SHORT).show(); }" – NewbieontheSide Feb 26 '13 at 18:59
  • now it doesn`t prompt anything at all, i guess i got wrong in my codes – NewbieontheSide Feb 26 '13 at 19:00
  • @NewbieontheSide use setTag and getTag comparision, or setContentDescription and getContentDescription for the imageviews.. – Pragnani Feb 26 '13 at 19:07
  • i hvae seen this link http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view but i don`t know how to implement it in my code.. sorry i`m new in android/java programming – NewbieontheSide Feb 26 '13 at 19:19
  • In your xml add attribute android:contentDescription="airplane" any name that you can identify..give drawable name for comparision and than compare like this if(iv.getcontnentDescription.equals("airplace")){//do somehting} – Pragnani Feb 26 '13 at 19:22
  • i`ve tried zabador and it works thanks does that code loads memory on phone for loading images?? cause i`m afraid that i might get outofmemory error in android – NewbieontheSide Feb 26 '13 at 19:30
  • You are using the images in the your drawable folders, Android pick the suitable image for the phone resolution...Not in every where , even in desk top , laptop need ram to show images.. You won't get outofmemory error untill you are using very high resolution images that your phone can't handle – Pragnani Feb 26 '13 at 19:34
  • i`m trying with atleast 10 images, but if i resize the images lower than let`s say 150kb will it be ok?? – NewbieontheSide Feb 26 '13 at 19:36
0

You are comparing ImageView instances and ints, the right way to check which view was clicked is:

if (iv1.getId() == R.id......) { ...

this way you are comparing an int with another int

gpasci
  • 1,420
  • 11
  • 16