What method should I call to know if an Activity has its contentView (once the method setContentView() has been called)?
287
votes
8 Answers
534
votes
23
votes
5
votes
2
votes
1
votes
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
votes
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.