How to get Activity's content view?

Android

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

or

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

or

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

Solution 2 - Android

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

Solution 3 - Android

How about

View view = Activity.getCurrentFocus();

Solution 4 - Android

You may want to try View.getRootView().

Solution 5 - Android

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

Solution 6 - Android

If you are using Kotlin

    this.findViewById<View>(android.R.id.content)
                         .rootView
                         .setBackgroundColor(ContextCompat.getColor(this, R.color.black))

Solution 7 - Android

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.

Solution 8 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionteoREtikView Question on Stackoverflow
Solution 1 - AndroidernestView Answer on Stackoverflow
Solution 2 - AndroidpapachanView Answer on Stackoverflow
Solution 3 - Androidmike jonesView Answer on Stackoverflow
Solution 4 - AndroidWill TateView Answer on Stackoverflow
Solution 5 - AndroidChrisView Answer on Stackoverflow
Solution 6 - AndroidHitesh SahuView Answer on Stackoverflow
Solution 7 - AndroidsergeytchView Answer on Stackoverflow
Solution 8 - AndroidAlejandro TellezView Answer on Stackoverflow