How to Hide a Fragment included in a BaseActivity layout from a specific Activity

446 Views Asked by At

Here is my issue, I am working on the Ads integration for my Mobile Application.

In order to display a banner, I have created a BannerFragment.

I want to display this banner in all of my Activities, so that I have included it in a ActionBarAbstractActivity layout.

Result on the screen

Code Layout

This allow me to display my Banner on all of my Activities without working on their XML files.

But, I have one specific Activity where I don't want to display Ads.

That's why I need to hide the Banner in this Activity. How can I access the BannerFragment in this Activity even if it's not referenced in its XML layout ?

I tried to find the Fragment by ID and hide it, but it's not working.

Should I create my proper banner container for this activty and set the visibility to "HIDE" or "GONE" ?

2

There are 2 best solutions below

0
Praveen Kishore On BEST ANSWER

You can change the visibility property of the linear layout itself to hide/show the fragment.

Use an id for the LinearLayout in the XML.

<LinearLayout 
    android:id="@+id/bannerLayout"
    android:layout_width="match_parent"
    android:layout_height="50dp">
    <fragment
     ..
     .. 
    />

</LinearLayout>

In your activity,

LinearLayout  bannerLayout = (LinearLayout) findViewById(R.id.bannerLayout);

To hide the layout,

bannerLayout.setVisibility(View.INVISIBLE); // Or in this case, View.GONE will also work

To show the layout,

bannerLayout.setVisibility(View.VISIBLE);
0
xzwc On

Maybe you can use include the xml of fragment instead of hide or gone.

like this

/>