How to show same ad banner on different screens?

跟風遠走 提交于 2019-12-06 15:02:31

I put Admob in its own fragment and just reuse that fragment across activities.

For someone who want the Demo code, I implement this in my apps.

Use one Activity + multiple Fragments

  1. Create a MainActivity, this activity manage all the fragments. in layout file below, the FrameLayout is the container of your fragments(to show the actual content of your app), and the fragment is the container of the Admob Ad banner.

===

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/adFragment"/>
        <fragment
            android:id="@+id/adFragment"
            android:name="com.jiyuzhai.wangxizhishufazidian.MainActivity$AdFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
</RelativeLayout>

===

  1. Add the first fragment in MainActivity, this is the first screen when you app start.

==

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new MainFragment())
                    .commit();
        }
    }
}

==

  1. Switch the fragment when you want, and the banner will be there(at the bottom of all the screen) all the time.

==

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack("null");
fragmentTransaction.commit();

==

Layout of the Ad banner

==

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

==

Note: Be sure you really want to do that when using this approach, for example, that's a not good user experience to show a banner at some page like Settings and About. you can easily hide/show the Ad banner by settings the visibility of the AdView to VISIBLE/INVISIBLE/GONE.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!