Android get type of a view
Asked Answered
A

7

68

How can i do this?

something:

final View view=FLall.getChildAt(i);

if (view.getType()==ImageView) {
...
}
Anny answered 7/11, 2010 at 14:8 Comment(0)
C
159

If, for some strange reason, you can't use Asahi's suggestion (using tags), my proposition would be the following:

if (view instanceof ImageView) {
    ImageView imageView = (ImageView) view;
    // do what you want with imageView
}
else if (view instanceof TextView) {
    TextView textView = (TextView) view;
    // do what you want with textView
}
else if ...
Convenance answered 7/11, 2010 at 14:43 Comment(3)
It doesn't work for ListView. And, I have to create another custom CustomListView which extends ListView, so that I can use (view instanceof CustomListView). Is there any way to know the type when it is ListView? Thanks.Oriane
@Oriane if view instanceof ListView is false, it means your view is not a ListView.Convenance
@Convenance - actually if you have a ListView and you have set "singleChoice" then it will return a class name of "android.widget.CheckedTextView". This has just bit me. This does this under the covers. :(Gradual
F
32

I tried the following and it worked:

View view = FLall.getChildAt(i);
Log.i("ViewName", view.getClass().getName());

For Kotlin, the equivalent code is

val view: View = FLall.getChildAt(i);
Log.i("ViewName", view.javaClass.name);
Fusspot answered 26/6, 2011 at 12:7 Comment(1)
I am using a TextView to set the style for a spinner (link in this example: developer.android.com/resources/tutorials/views/…). I am also utilizing the onItemSelected event method for the spinner. The event kept triggering with a a view that I didn't recognize. Using your method above -- v.getClass().getName() -- I was able to determine that the View being passed into the event is the TextView I'm using for the styles.Rowdyish
C
10

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; 
    }
Copaiba answered 25/3, 2014 at 9:8 Comment(0)
K
7

You can use tag for that purpose:see set/getTag methods at http://developer.android.com/reference/android/view/View.html

Kight answered 7/11, 2010 at 14:40 Comment(0)
O
0

I am using this solution for KOTLIN code, so going off of Arash's solution:

if(v.javaClass.name.equals("android.widget.ImageView", ignoreCase = true)) ...

using this didn't work for me, but tweaking it to:

if(v.javaClass.name.contains("ImageView", ignoreCase = true)) ...

worked for me!

Overlie answered 28/5, 2020 at 7:20 Comment(0)
B
0

In Kotlin you can check this by using "is":

val view = findViewById<View>(R.id.a_view)

if (view is ImageView) {
    print("This is a ImageView")
} else {
    print("This is not a ImageView")
}
Blastocyst answered 27/8, 2021 at 11:11 Comment(0)
B
0

In Android Xamarin, This is how Android get type of a view and compare with controller type.

var view = FindViewById<TextView>(Resource.Id.emailText);

if (typeof(TextView).IsInstanceOfType(view))
{
    var textView = (TextView)view;
}
Barquisimeto answered 16/1, 2022 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.