Why is there no getContentView () method for an Activity?

The Activity class has a setContentView() method. The PopupWindow class has a getContentView() method, but does nothing. Is there any other way to get a basic view of content for activity?

+43
android android view
Sep 17 '10 at 14:45
source share
3 answers

I managed to get to the contents of the Activity using this call:

 ViewGroup view = (ViewGroup)getWindow().getDecorView(); 

You should probably check that getDecorView returns a ViewGroup instance for all cases, but with the LinearLayout in Activity, the code above works fine. To get to LinearLayout, you could simply:

 LinearLayout content = (LinearLayout)view.getChildAt(0); 

And if you have a function like this:

 void logContentView(View parent, String indent) { Log.i("test", indent + parent.getClass().getName()); if (parent instanceof ViewGroup) { ViewGroup group = (ViewGroup)parent; for (int i = 0; i < group.getChildCount(); i++) logContentView(group.getChildAt(i), indent + " "); } } 

You can iterate over all views and write down the names of their classes with the following call inside your activity:

 logContentView(getWindow().getDecorView(), ""); 
+48
Nov 21 2018-10-11T00:
source share

The following line will do the trick:

 findViewById(android.R.id.content); 

it is essentially the same as (it needs to be called in the context of the Activity)

 this.findViewById(android.R.id.content); 
+42
Jan 07 '13 at 5:42
source share

I am looking for this too, but I just thought that adding an external external ViewGroup might be easier.

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/outer"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> 

I will continue to search for a few more minutes. I go into this to use findViewWithTag from the outermost layout.

+3
Nov 21 '10 at 5:52
source share



All Articles