What method should I call to know if an Activity has its contentView (once the method setContentView() has been called)?
Asked
Active
Viewed 2.5e+01k times
8 Answers
544
this.getWindow().getDecorView().findViewById(android.R.id.content)
or
this.findViewById(android.R.id.content)
or
this.findViewById(android.R.id.content).getRootView()

ernest
- 5,743
- 1
- 15
- 7
-
5or `getWindow().findViewById(android.R.id.content)` :) – Alex Lockwood Jun 26 '13 at 01:54
-
1this works fine as far as fetching the Parent view is concerned... BUt..if i try this.findViewById(android.R.id.content).setBackgroundDrawable(d); it dosent work.. Can you please suggest.. Thanks in advance – Rajesh Wadhwa Aug 02 '13 at 09:08
-
3If you install a layout from XML using `setContentView(R.layout.my_view)`, this returns the *parent* of that layout. – Jay Lieske Aug 26 '13 at 23:46
-
Hello @ernest I have a sort question like If in my layout i have one image on top than two tab in tab bar. and in bottom a SatelliteMenu, Want to do blur on menu items are opened. – Devendra Singh May 12 '15 at 07:30
-
Sorry if I am wrong but @Jay Lieske explanation should be totally added to the answer (I leave the edit to him as he made the point) – DNax Jul 23 '15 at 14:25
-
Be careful. Calling [getDecorView](http://developer.android.com/reference/android/view/Window.html#getDecorView%28%29) before `setContentView` might prematurely “lock in various window characteristics”. So might calling `findViewById` ([bug 199710](https://code.google.com/p/android/issues/detail?id=199710)). – Michael Allan Jan 27 '16 at 17:16
-
would you help me out to get view of fragment , after updateLocale( ) or onResume in Activity, – Abdul Wahab Aug 02 '17 at 20:27
-
1content doesn't exist for Android.Resource.Id. – Justin Apr 04 '18 at 14:21
-
for kotlin use property call like `window.decorView.rootView` :) – QuarK Jan 31 '19 at 13:05
23
You can get the view Back if you put an ID to your Layout.
<RelativeLayout
android:id="@+id/my_relative_layout_id"
And call it from findViewById ...

papachan
- 1,891
- 17
- 20
-
This is the most robust way to get the root of an installed layout. – Jay Lieske Aug 26 '13 at 23:47
-
3
-
2@trans View contentView = findViewById(R.id.my_relative_layout_id); – Justin Liu Jan 13 '17 at 03:04
13
How about
View view = Activity.getCurrentFocus();

mike jones
- 649
- 4
- 15
-
-
1@aeskreis because in my case it returns null `currentFocus?.let { ... } ` – user924 Apr 05 '19 at 12:32
5
You can also override onContentChanged()
which is among others fired when setContentView()
has been called.

Chris
- 7,675
- 8
- 51
- 101
-
ernest's answer will give you the current focussed view even if contains scrollable view – YuDroid Sep 08 '12 at 14:06
3
If you are using Kotlin
this.findViewById<View>(android.R.id.content)
.rootView
.setBackgroundColor(ContextCompat.getColor(this, R.color.black))

Hitesh Sahu
- 41,955
- 17
- 205
- 154
1
There is no "isContentViewSet" method. You may put some dummy requestWindowFeature call into try/catch block before setContentView like this:
try { requestWindowFeature(Window.FEATURE_CONTEXT_MENU); setContentView(...) } catch (AndroidRuntimeException e) { // do smth or nothing }
If content view was already set, requestWindowFeature will throw an exception.

sergeytch
- 161
- 7
-3
The best option I found and the less intrusive, is to set a tag param in your xml, like
PHONE XML LAYOUT
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="phone"/>
TABLET XML LAYOUT
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="tablet">
...
</RelativeLayout>
and then call this in your activity class:
View viewPager = findViewById(R.id.pager);
Log.d(getClass().getSimpleName(), String.valueOf(viewPager.getTag()));
Hope it works for u.

Alejandro Tellez
- 161
- 1
- 4