293

What method should I call to know if an Activity has its contentView (once the method setContentView() has been called)?

PLNech
  • 3,087
  • 1
  • 23
  • 52
teoREtik
  • 7,886
  • 15
  • 46
  • 65

8 Answers8

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
  • 5
    or `getWindow().findViewById(android.R.id.content)` :) – Alex Lockwood Jun 26 '13 at 01:54
  • 1
    this 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
  • 3
    If 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
  • 1
    content 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
13

How about

View view = Activity.getCurrentFocus();
mike jones
  • 649
  • 4
  • 15
11

You may want to try View.getRootView().

nicael
  • 18,550
  • 13
  • 57
  • 90
Will Tate
  • 33,439
  • 9
  • 77
  • 71
5

You can also override onContentChanged() which is among others fired when setContentView() has been called.

Chris
  • 7,675
  • 8
  • 51
  • 101
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.