native admob banner ad

49 views
Skip to first unread message

Fuad Nassar

unread,
Jun 22, 2019, 10:07:54 PM6/22/19
to CodenameOne Discussions
I tried using a banner AdMob ad with using these hint android.googleAdUnitId and i need to add something like these  features on this ad 

firstly: the background color of ad is black and I need to replace it to white


secondly: the ad will appear starting from splash screen and that it's not beautiful. I want to avoid appear it on this form.


thirdly:  I want to specify the location of the ad  between component

how can i make all these things as native code with using native interface?

i see this topic for add banner in android studio without using xml file 

and i writen these code as native code 
package com.soft.test;
import com.codename1.impl.android.AndroidNativeUtil;
import android.app.Activity;
import android.content.Context;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.AdSize;
import android.widget.LinearLayout;
import android.view.Gravity;

public class BannerInterImpl {
    Context context = AndroidNativeUtil.getContext();
    Activity activity = AndroidNativeUtil.getActivity();

    public void showBannerAd(String id) {
        //Add this wherever your code needs to add the ad

LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);

//Additionally to adjust the position to Bottom
layout.setGravity(Gravity.BOTTOM);

// Create a banner ad
AdView mAdView = new AdView(context);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

// Add the AdView to the view hierarchy.
layout.addView(mAdView);

// Start loading the ad.
mAdView.loadAd(adRequestBuilder.build());

activity.setContentView(layout);
    }

    public void initAd() {
        MobileAds.initialize(context, "ca-app-pub-3940256099942544/6300978111");
    }

    public boolean isSupported() {
        return true;
    }

}


and this in the main class 
       Form hi = new Form("Hi World", BoxLayout.y());
        hi.add(new Label("Hi World"));
        hi.show();
        banner.initAd();
        banner.showBannerAd("");

but that doesn't work and app will not add the ad on the main class

Shai Almog

unread,
Jun 22, 2019, 11:20:44 PM6/22/19
to CodenameOne Discussions
This support is pretty old and we no longer maintain it. A better approach is available in the full screen admob API: https://github.com/shannah/admobfullscreen-codenameone/

You can probably adapt that code to support banner ads with a peer component. This will give you full control over the banner placement/background etc. You can use a peer component to pass a view from native back to the main UI and wrap it.
Reply all
Reply to author
Forward
0 new messages