How to check the view widget view?

How to do it:

if(findViewById(ResIdWithUnkownType) instanceof Bitmap) { Bitmap bitmap = (Bitmap) findViewById(ResIdWithUnkownType); } else if(findViewById(ResIdWithUnkownType) instanceof ImageView) { ImageView = (ImageView) findViewById(ResIdWithUnkownType); } 
+6
android android-layout android-widget imageview
source share
2 answers

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

+6
source share

This is not an answer, but for others who check this question, instanceof does not work in some cases (I don’t know why!), For example, if you want to check if the view type is ImageView or ImageButton (I tested this situation), they will get they are the same, so you scan it like this:

 //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; } 
+3
source share

All Articles