I'm new to InMobi and I'm trying to implement a banner ad to my app.
Here's my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpBannerAd();
}
private void setUpBannerAd(){
InMobiSdk.init(MainActivity.this, MY_ID);
InMobiBanner bannerAd = findViewById(R.id.banner);
bannerAd = new InMobiBanner(MainActivity.this, BANNER_ID);
bannerAd.setListener(new InMobiBanner.BannerAdListener() {
#Override
public void onAdLoadSucceeded(InMobiBanner inMobiBanner) {
Toast.makeText(MainActivity.this, "sucess", Toast.LENGTH_SHORT).show();
}
#Override
public void onAdLoadFailed(InMobiBanner inMobiBanner,
InMobiAdRequestStatus inMobiAdRequestStatus) {
Toast.makeText(MainActivity.this, "fail", Toast.LENGTH_SHORT).show();
}
#Override
public void onAdDisplayed(InMobiBanner inMobiBanner) {
}
#Override
public void onAdDismissed(InMobiBanner inMobiBanner) {
}
#Override
public void onAdInteraction(InMobiBanner inMobiBanner, Map<Object, Object> map) {
}
#Override
public void onUserLeftApplication(InMobiBanner inMobiBanner) {
}
#Override
public void onAdRewardActionCompleted(InMobiBanner inMobiBanner, Map<Object, Object> map) {
}
});
bannerAd.load();
}
}
I think i have all the needed imports as well as manifest declarations, but not entirely sure because I'm new to the platform. Test ads are turned on. Toasts do NOT even show up.
Thanks for replies!
You need to add the banner to the view hierarchy once its created. Something like this:
RelativeLayout adContainer = (RelativeLayout) findViewById(R.id.ad_container);
RelativeLayout.LayoutParams bannerLp = new
RelativeLayout.LayoutParams(640, 100);
bannerLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bannerLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
adContainer.addView(bannerAd, bannerLayoutParams);</code>
It looks like you created the banner twice. Once in XML, and once in code:
//the banner is created below by reference to xml
InMobiBanner bannerAd = findViewById(R.id.banner);
//the banner is created below in code
bannerAd = new InMobiBanner(MainActivity.this, BANNER_ID);</code>
Check out this guide for more details: https://support.inmobi.com/monetize/android-guidelines/banner-ads-for-android/
Related
I am trying to implement interstitial ad in a fragment, when button is clicked the fragment doesn't open other activity , but loads data in same fragment so i can't use the they way people say interstisial ad should be used when navigating between activities. instead i am using the ad for only once when button is clicked .it works but takes sometime .how to deal with it?
it only works if user pressed the button after few seconds of launching the app.
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_home, container, false);
Button btn=root.findViewById(R.id.btn);
initAds();
btn.setOnClickListener(this);
return root;
}
#Override
public void onClick(View view){
initAdsCallBack();
}
private void initAds(){
MobileAds.initialize(requireActivity(), new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(#NonNull InitializationStatus initializationStatus) {
createNonPersonalisedAd();
}
});
}
}
private void createNonPersonalisedAd() {
Bundle networkExtrasBundle = new Bundle();
networkExtrasBundle.putInt("rdp", 1);
AdManagerAdRequest adRequest = (AdManagerAdRequest) new AdManagerAdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, networkExtrasBundle)
.build();
createInterstitialAd(adRequest);
}
private void createInterstitialAd(AdRequest adRequest){
AdManagerInterstitialAd.load(requireActivity(),getResources().getString(R.string.str_iterstitial), (AdManagerAdRequest) adRequest,
new AdManagerInterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull AdManagerInterstitialAd interstitialAd) {
// The mAdManagerInterstitialAd reference will be null until
// an ad is loaded.
mAdManagerInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
///// best place to callback is here coz its successfully loaded here
mAdManagerInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
#Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d("TAG", "The ad was dismissed.");
createInterstitialAd(adRequest);
}
#Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d("TAG", "The ad failed to show.");
}
#Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
mAdManagerInterstitialAd = null;
Log.d("TAG", "The ad was shown.");
}
});
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
// Handle the error
Log.i(TAG, loadAdError.getMessage());
mAdManagerInterstitialAd = null;
}
});
}
private void initAdsCallBack(){
if (mAdManagerInterstitialAd != null) {
mAdManagerInterstitialAd.show(requireActivity());
debugToast("ad shown");
} else {
Log.e("Tad didn't show");
}
}
Consider pre-loading your interstitial ads to reduce latency when displaying them to your users. For more information about pre-loading your interstitial ads refer to the AdMob Interstitial Ad developer guidelines for apps developed for Android
I have a fragment that contains a listview , I want to show interstial ad whene the user click a listview item , I tried the following code but it is not showing :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
......
mInterstitialAd = new InterstitialAd(mContext);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}
});
......
return rootView;
}
Set up new class which have set, load and show interstatialAd static methods as below:
public class AdSetup {
// Application context returned by function --> Application.get()
public static String TesttingDeviceID = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
final static InterstitialAd mInterstitialAd = new InterstitialAd(Application.get());
public static void setInterstitial() {
try {
if (mInterstitialAd.getAdUnitId() == null) {
mInterstitialAd.setAdUnitId(Application.get().getString(R.string.interstitialAd));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void loadInterstatial() {
try {
setInterstitial();
AdRequest adRequestInter = new AdRequest.Builder().build();
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Log.e("mInterstetial onloaded", "onloaded isLoaded" + mInterstitialAd.isLoaded());
}
#Override
public void onAdFailedToLoad(int i) {
Log.e("mInterstetial onloaded", "onloaded onAdFailedToLoad");
}
#Override
public void onAdOpened() {
// super.onAdOpened();
}
#Override
public void onAdClosed() {
// super.onAdClosed();
}
#Override
public void onAdClicked() {
// super.onAdClicked();
}
});
mInterstitialAd.loadAd(adRequestInter);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void showInterstatial() {
try {
Log.e("mInterstetial show ", "isLoaded" + mInterstitialAd.isLoaded());
if (mInterstitialAd.isLoaded() && mInterstitialAd.getAdUnitId() != null) {
mInterstitialAd.show();
} else {
loadInterstatial();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
How to call this methods on click of the list item or the onCreate() of the new Activity to comply with the interstatial ad policy:
AdSetup.showInterstatial(); // Show ad if already loaded else load Ad
AdSetup.loadInterstatial(); // Load new ad
Make sure to follow the policies listed in the below links:
Disallowed interstitial implementations
App load or exit Do not place interstitial ads on app load and when
exiting apps as interstitials should only be placed in between pages
of app content. Ads should not be placed in applications that are
running in the background of the device or outside of the app
environment. It should be clear to the user which application the ad
is associated with or implemented on.
Recommended interstitial implementations
Also, add Application class:
public class Application extends android.app.Application {
// public static DeviceName.DeviceInfo deviceInfo;
private static Application _instance;
#Override
public void onCreate() {
super.onCreate();
_instance = this;
}
//return the App context
public static Application get() {
return _instance;
}
}
Also declare the Application class in your AndroidManifest.xml as you can see below first line:
<application
android:name=".Application"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:allowBackup="false"
android:supportsRtl="true"
android:networkSecurityConfig="#xml/network_security_config"
android:theme="#style/AppTheme">
I just implemented this in my code. It works. In the example that you gave I did not see if you implemented AdListener after AdRequest.Builder. If not, here is an example of that:
ExampleInterstitial.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
ExampleInterstitial.loadAd(new AdRequest.Builder().build());
}
});
Next, it appeared in your example that you just have the interstitial ad to show onItemClick but do you have an intent to move to a new activity also set there? I didn't see it. Check out my code below. I set the intent to start the new activity then I show the add after. Just a thought because this works for me. Good luck!
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent;
intent = new Intent(getActivity(), ExampleActivity.class);
startActivity(intent);
if (ExampleInterstitial.isLoaded()) {
ExampleInterstitial.show();
}
else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
Hello I am trying to integrated Banner Ad using RevMob in Libgdx. But it is not displaying for some reason.
I am using the following code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_game_new);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
// cfg.useGL20 = false;
final RelativeLayout gameLayout = new RelativeLayout(this);
RevMobIntegration revmob = new RevMobIntegration(this);
RelativeLayout bannerLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_VERTICAL);
bannerLayout.setLayoutParams(adParams);
game = new MyGdxGame(GameActivity.this, revmob);
game.setRedirectionListener(this);
View gameView = initializeForView(game, cfg);
requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Add the libgdx view
gameLayout.addView(gameView);
// gameLayout.addView(bannerLayout);
Log.d("RevMob", "Checking BannerAd");
if (revmob.getBannerAd() != null) {
Log.d("RevMob", "Displaying Called");
gameLayout.addView(revmob.getBannerAd());
}
// Hook it all up
setContentView(gameLayout);
this.onPause();
this.onResume();
gameLayout.refreshDrawableState();
initChartboost();
// startRevMobSession();
}
Here is the RevMobIntegration class :
public class RevMobIntegration implements RevmobAdInterface {
private static final String APPLICATION_ID = "YourAdmobAppIDHere";
// Set this to false when creating the version for the store.
private static final boolean DEBUG = true;
private RevMobAdsListener listener;
private RevMobFullscreen fullscreenAd;
private RevMobBanner bannerAd;
private Activity application;
private RevMob revmob;
public RevMobIntegration(Activity _application) {
this.application = _application;
startRevMobSession();
}
public void startRevMobSession() {
//RevMob's Start Session method:
revmob = RevMob.startWithListener(application, new RevMobAdsListener() {
#Override
public void onRevMobSessionStarted() {
loadBanner(); // Cache the banner once the session is started
Log.i("RevMob", "Session Started");
}
#Override
public void onRevMobSessionNotStarted(String message) {
//If the session Fails to start, no ads can be displayed.
Log.i("RevMob", "Session Failed to Start");
}
}, application.getString(R.string.rev_mob_app_id));
}
//RevMob
public void loadBanner() {
bannerAd = revmob.preLoadBanner(application, new RevMobAdsListener() {
#Override
public void onRevMobAdReceived() {
showBannerAd(true);
Log.i("RevMob", "Banner Ready to be Displayed"); //At this point,
the banner is ready to be displayed.
}
#Override
public void onRevMobAdNotReceived(String message) {
Log.i("RevMob", "Banner Not Failed to Load");
}
#Override
public void onRevMobAdDisplayed() {
Log.i("RevMob", "Banner Displayed");
}
});
}
#Override
public void showBannerAd(boolean show) {
if(show) {
Log.i("RevMob", "Showing");
if(bannerAd == null) {
startRevMobSession();
} else {
Log.i("RevMob", "Banner Displayed");
bannerAd.show();
}
} else {
bannerAd.hide();
}
}
public RevMobBanner getBannerAd() { return bannerAd; }
}
I have integrated the RevMob in my Activities and it is working fine. But for the Game Screen the ad is initializing but not displaying.
Any suggestions?
It seems revmob.getBannerAd() return null because bannerAd object created when loadBanner(); called. RevMob take some time to start it's session.
if (revmob.getBannerAd() != null) {
Log.d("RevMob", "Displaying Called");
gameLayout.addView(revmob.getBannerAd());
}
You can check this repo for clarification also you can take a look of this class.
I'm having a problem with vungle, I want the dialog fragment to appear after the video ad ends, my code looks good and there is no errors, the video starts and ends but my dialog fragment never appears, here is my code:
private final EventListener vungleListener = new EventListener(){
#Override
public void onVideoView(boolean isCompletedView, int watchedMillis, int videoDurationMillis) {
if (isCompletedView){
frag.show(fm, "");
}
}
#Override
public void onAdStart() {
// Called before playing an ad
}
#Override
public void onAdEnd(boolean wasCallToActionClicked) {
}
#Override
public void onAdPlayableChanged(boolean isAdPlayable) {
}
#Override
public void onAdUnavailable(String reason) {
}
};
According to Vungle the method onVideoView is deprecated and the method onAdEnd should be used instead. You should try moving the fragment being shown to onAdEnd like below:
#Override
public void onAdEnd(boolean wasCallToActionClicked) {
frag.show(fm, "");
}
the issue is: the ad doesn't show on first request, but when it makes the second request, it shows right. About 2 seconds before it make the second request, the ad of the first request shows up. Any ideas?
Thanks,
EDIT: I changed the gravity to TOP, and now it shows on the first time, but is showing just the top half of the ad, bizarre, any ideas?
#Override
protected void onSetContentView() {
if(adView != null){
return;
}
final FrameLayout frameLayout = new FrameLayout(this);
final FrameLayout.LayoutParams frameLayoutLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
final FrameLayout.LayoutParams adViewLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(getResources().getString(R.string.ad_unit_id));
adView.setAdListener(new ToastAdListener(this));
adView.loadAd(new AdRequest.Builder().build());
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine,this);
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
frameLayout.addView(adView, adViewLayoutParams);
this.setContentView(frameLayout, frameLayoutLayoutParams);
}
#Override
protected void onPause() {
if(adView!=null){
adView.pause();
}
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
if(adView!=null){
adView.resume();
}
}
#Override
protected void onDestroy() {
if(adView!=null){
adView.destroy();
}
super.onDestroy();
}
The problem was solved for me by adding the line
adView.setBackgroundColor(android.graphics.Color.TRANSPARENT);
The way I have done is as follows:
In the method which changes the visibility, I first check is the ad was loaded. If it was not, I don't change the visibility
Set an AdListener for the AdView. in the onReceivedAd(), I check the condition to hide it - if it should be hidden, I hide.
Works fine this way.
I have got the same problem, I solved this way:
Advertisement class:
public class Advertisement {
private AdView adView;
private final Handler adsHandler = new Handler();
public Advertisement(final Activity activity) {
adView = (AdView)activity.findViewById(R.id.adView);
}
//show the ads.
private void showAds () {
adView.loadAd(new AdRequest.Builder().build());
adView.setVisibility(android.view.View.VISIBLE);
adView.setEnabled(true);
}
//hide ads.
private void unshowAds () {
adView.loadAd(new AdRequest.Builder().build());
adView.setVisibility(android.view.View.INVISIBLE);
adView.setEnabled(false);
}
final Runnable unshowAdsRunnable = new Runnable() {
public void run() {
unshowAds();
}
};
final Runnable showAdsRunnable = new Runnable() {
public void run() {
showAds();
}
};
public void showAdvertisement() {
adsHandler.post(showAdsRunnable);
}
public void hideAdvertisement() {
adsHandler.post(unshowAdsRunnable);
}
}
In code:
...
public static Advertisement advertisement = new Advertisement(this);
...
public static void showAd()
{
if (advertisement != null)
advertisement.showAdvertisement();
}
public static void hideAd()
{
if (gangsterAdvertisement != null)
advertisement.hideAdvertisement();
}