293

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

8 Answers 8

544
this.getWindow().getDecorView().findViewById(android.R.id.content)

or

this.findViewById(android.R.id.content)

or

this.findViewById(android.R.id.content).getRootView()
9
  • 5
    or getWindow().findViewById(android.R.id.content) :) Jun 26, 2013 at 1: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 Aug 2, 2013 at 9: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, 2013 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. May 12, 2015 at 7:30
  • 1
    content doesn't exist for Android.Resource.Id.
    – Justin
    Apr 4, 2018 at 14:21
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 ...

3
  • This is the most robust way to get the root of an installed layout.
    – Jay Lieske
    Aug 26, 2013 at 23:47
  • 3
    What do you pass to findViewById ?
    – trans
    Jun 16, 2016 at 15:12
  • 2
    @trans View contentView = findViewById(R.id.my_relative_layout_id);
    – Justin Liu
    Jan 13, 2017 at 3:04
13

How about

View view = Activity.getCurrentFocus();
2
  • How does this not have more thumbs up? Works great!
    – aeskreis
    Oct 22, 2018 at 18:38
  • 1
    @aeskreis because in my case it returns null currentFocus?.let { ... }
    – user924
    Apr 5, 2019 at 12:32
11

You may want to try View.getRootView().

5

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

1
  • ernest's answer will give you the current focussed view even if contains scrollable view
    – YuDroid
    Sep 8, 2012 at 14:06
3

If you are using Kotlin

    this.findViewById<View>(android.R.id.content)
                         .rootView
                         .setBackgroundColor(ContextCompat.getColor(this, R.color.black))
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.

-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.

1
  • 4
    How does this answer the question?
    – Onik
    May 6, 2014 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.