6

How to get that working:

 if(findViewById(ResIdWithUnkownType) instanceof Bitmap)
 {
       Bitmap bitmap = (Bitmap) findViewById(ResIdWithUnkownType);
 } else if(findViewById(ResIdWithUnkownType) instanceof ImageView)
 {
       ImageView = (ImageView) findViewById(ResIdWithUnkownType);
 }
OneWorld
  • 17,512
  • 21
  • 86
  • 136
  • 1
    On an unrelated note, you'd be better off saving the `View` object before checking it's type rather than calling `findViewById` over and over again... – Chris Thompson Oct 05 '10 at 14:45

2 Answers2

6

The second block would work just fine. The problem is the first one: findViewById returns a View object always, and Bitmap is not a View, so the first if statement will never be executed.

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Thanks for the quick answer. Actually, it's related to this, if u want to look at it: http://stackoverflow.com/questions/3864977/android-how-to-exchange-an-image-in-a-layer-list-by-a-bitmap – OneWorld Oct 05 '10 at 15:09
3

This is not the answer but For Others who check this Question,in some cases instanceof does not work(I do not know why!),for example if your want to check if view type is ImageView or ImageButton(i tested this situation) , it get them the same, so you scan use this way :

//v is your View
    if (v.getClass().getName().equalsIgnoreCase("android.widget.ImageView")) {
        Log.e("imgview", v.toString());
        imgview = (ImageView) v;
    } else if (v.getClass().getName().equalsIgnoreCase("android.widget.ImageButton")) {
        Log.e("imgbtn", v.toString());
        imgbtn = (ImageButton) v; 
    }
Arash
  • 3,013
  • 10
  • 52
  • 74