Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get type of a view

How can i do this?

something:

final View view=FLall.getChildAt(i);  if (view.getType()==ImageView) { ... } 
like image 760
lacas Avatar asked Nov 07 '10 14:11

lacas


People also ask

How do I know the type of view?

final View view=FLall. getChildAt(i); if (view. getType()==ImageView) { ... }

How do I get text from view?

For example, if the View contains text, use this: ( (TextView) view ). getText(). toString();

What does findViewById return?

findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .

What is the use of findViewById in Android?

FindViewById<T>(Int32)Finds a view that was identified by the id attribute from the XML layout resource.


1 Answers

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 ... 
like image 146
Felix Avatar answered Sep 22 '22 10:09

Felix