-5
if( txt.equals("Apple"))
  {
        Toast.makeText(this, "Times up", Toast.LENGTH_SHORT).show();
        AlertDialog ad = new AlertDialog.Builder(this).create();  
        ad.setCancelable(false); // This blocks the 'BACK' button  
        ad.setMessage("You win");  
        ad.setButton("OK", new DialogInterface.OnClickListener() {  
           @Override  
          public void onClick(DialogInterface dialog, int which) {  
              dialog.dismiss();                      
            }  
        });

       ad.show(); 

  }

what should i write to compare Apple text with the image of imageview

  • Imagview img=new (Imageview)findviewbyid(R.id.fetchimage); – QuokMoon Dec 22 '13 at 17:16
  • i also want it to use in IF Statement – user3127549 Dec 22 '13 at 17:17
  • @user3127549 use startActivity for result. But your post is not clear to me – Raghunandan Dec 22 '13 at 17:18
  • @Raghunandan actually i want to compare the image of imageview with the text of edittext for example if i write Apple in edittext and there is also the picture of apple in the imageview only then success message should be pop out – user3127549 Dec 22 '13 at 17:21
  • if( txt.equals("Apple")) { Toast.makeText(this, "Times up", Toast.LENGTH_SHORT).show(); AlertDialog ad = new AlertDialog.Builder(this).create(); ad.setCancelable(false); // This blocks the 'BACK' button ad.setMessage("You win"); ad.setButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); ad.show(); } WHAT SHOULD I WRITE CO COMPARE THE IMAGEVIEW IMAGE WITH EDITTEXT TEXT – user3127549 Dec 22 '13 at 17:23

2 Answers2

1

what i see in your code, you are trying to compare two drawables using equals method. i don't think that is going to work.

to compare two drawables, one easy hack is to convert them to bitmaps and check using ==

Drawable fDraw = iv1.getDrawable();
Drawable sDraw = getResources().getDrawable(R.drawable.mario_pinball);

Bitmap bitmap = ((BitmapDrawable)fDraw).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)sDraw).getBitmap();

if(bitmap == bitmap2)
    {
        //Code blcok
    }

EDIT : based on comment, if above method does not work

Bitmap class provides a method sameAs() to compare two Bitmaps http://developer.android.com/reference/android/graphics/Bitmap.html#sameAs%28android.graphics.Bitmap%29

EDIT:

based on OPs comments on the question, to compare text with image, you can setTag() and getTag() in the image to store a text value, and then compare this value with the textview's text.

gaurav5430
  • 12,934
  • 6
  • 54
  • 111
  • I would not count on those returning the same `Bitmap` object in all circumstances. – CommonsWare Dec 22 '13 at 17:29
  • And your edit will be slow, as it has to iterate over all pixels for comparison purposes. – CommonsWare Dec 22 '13 at 17:38
  • @CommonsWare please see http://stackoverflow.com/questions/9125229/comparing-two-drawables-in-android and http://stackoverflow.com/questions/6120439/comparing-bitmap-images-in-android/7696320#7696320 – gaurav5430 Dec 22 '13 at 17:44
1

what should i write to compare Apple text with the image of imageview

You wouldn't.

Instead, maintain some other data member that reflects what you put in the ImageView (e.g., an int of the R.drawable value) whenever you change the ImageView. Then, you can check that other data member for equality.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491