Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all Views in an Activity?

is there a way to get every view that is inside my activity? I have over 200 views including buttons, and images, so i want to be able to access them by using a loop

for example something like

for (View v : this)
{
     //do something with the views 
     //depending on the types (button, image , etc)
}
like image 640
aryaxt Avatar asked Jul 10 '10 22:07

aryaxt


People also ask

What is a decor view?

The DecorView is the view that actually holds the window's background drawable. Calling getWindow(). setBackgroundDrawable() from your Activity changes the background of the window by changing the DecorView's background drawable.

What is SetContentView?

SetContentView(View) Set the activity content to an explicit view. SetContentView(Int32) Set the activity content from a layout resource. SetContentView(View, ViewGroup+LayoutParams)

What is an activity which method is implemented by all subclasses of an activity?

An activity represents a single screen with a user interface just like window or frame of Java. Android activity is the subclass of ContextThemeWrapper class. The Activity class defines the following call backs i.e. events. You don't need to implement all the callbacks methods.


2 Answers

To be specific:

private void show_children(View v) {
    ViewGroup viewgroup=(ViewGroup)v;
    for (int i=0;i<viewgroup.getChildCount();i++) {
        View v1=viewgroup.getChildAt(i);
        if (v1 instanceof ViewGroup) show_children(v1);
        Log.d("APPNAME",v1.toString());
    }
}

And then use the function somewhere:

show_children(getWindow().getDecorView());

to show all Views in the current Activity.

like image 22
Martin B Avatar answered Oct 14 '22 13:10

Martin B


is there a way to get every view that is inside my activity?

Get your root View, cast it to a ViewGroup, call getChildCount() and getChildAt(), and recurse as needed.

I have over 200 views including buttons, and images, so i want to be able to access them by using a loop

That is a rather large number of Views.

like image 173
CommonsWare Avatar answered Oct 14 '22 12:10

CommonsWare