Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Use same Google Admob Banner Ad Unit

spoduguspodugu Member ✭✭
edited October 22 in Xamarin.Android

Our application displays banner ad's in 4-5 pages. Is it ok to have same banner ad unit (same Ad unit Id) in all pages?

Tagged:

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    We can use one single activity. And use four different fragments to display your pages.
    Here is my main activity layout:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <!--banner ads here-->
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/frameLayout"/>
    </LinearLayout>
    

    Page one's fragment:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Page1"
            android:textAlignment="center"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Click"
            android:id="@+id/button"/>
    
    </LinearLayout>
    // code
    public class Page1 : AndroidX.Fragment.App.Fragment
    {
    
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            // Create your fragment here
        }
    
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            View view = inflater.Inflate(Resource.Layout.page1, container, false);
    
            Button btn = view.FindViewById<Button>(Resource.Id.button);
            btn.Click += (sender, e) =>
            {
                var transaction = this.FragmentManager.BeginTransaction();
                transaction.Replace(Resource.Id.frameLayout, new Page2());
                transaction.Commit();
            };
    
            return view;
        }
    }
    

    Show the first fragment using:

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
    
        var transaction = SupportFragmentManager.BeginTransaction();
        transaction.Replace(Resource.Id.frameLayout, new Page1());
        transaction.Commit();
    }
    

    The key point here is to use the frameLayout on the main activity to replace fragments. And leave the banner there on the top of the main page.

  • spoduguspodugu Member ✭✭

    @LandLu Thank you for your response.

    I am new to Xamarin, please bear with me. I have followed this article to incorporate ads into my ContentPages. Ads are displaying fine.

    https://montemagno.com/xamarinforms-google-admob-ads-in-android/

    The custom control is embedded in all Content Pages.

    My concern is since I am using this control everywhere which has same UnitId, will this be a problem.

Sign In or Register to comment.