Interstitial Ads and Banner ads not loading - android

I am loading banner ads and interstitial ads at different events. The problem is ads are loading fine at some places and at some place, they are not loading at all. I used same method to load the ads all over the application so why are they not loading at some points? Here is the code to interstitial ads which is working fine in splash screen :
setContentView(R.layout.splash);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.AdUnitId_interstitial));
requestNewInterstitial();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
requestNewInterstitial();
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
}
});
}
finish();
}
}, SPLASH_TIME_OUT);
}
The same code is not working in drawer activity. Here is the code in onCreate:
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.AdUnitId_interstitial));
requestNewInterstitial();
Here is the code for the button click handler:
else if (id == R.id.audio) {
startActivity(new Intent(MainActivity.this, AndroidBuildingMusicPlayerActivity.class));
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
requestNewInterstitial();
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
}
});
}
And for banner ads i am using this :
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest1 = new AdRequest.Builder().build();
mAdView.loadAd(adRequest1);
It works fine in a few activities and doesn't work in some. Anyone got a clue?

The problem was with my layout. It was not getting enough space for the adview to show. The Java code is absolutely fine.

Related

build AdMob Interstitial ad again after 60 seconds

Currently, I am using the code which is displaying AdMob Interstitial ad after 20 seconds and when the ad is closed I want to show another ad after 60 seconds, this is the code currently I am using for the first ad to load after 20 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
interAd = new InterstitialAd(MainActivity.this);
interAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
interAd.loadAd(adRequest);
interAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
interAd.show();
}
});
interAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Code to be executed when the interstitial ad is closed.
Log.i("Ads", "onAdClosed");
}
});
}
} , 20000);
i have tried this method from AdMob tutorial it load the ads but after every second
interAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Load the next interstitial.
interAd.loadAd(new AdRequest.Builder().build());
}
});
You should attach you Ad's lifecycle to your Activity's lifecycle so you can start/stop showing adds just when your app is visible to the user.
Create a global variables for you Ad and Handler
private InterstitialAd mInterstitialAd;
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
// Wait 60 seconds
mHandler.postDelayed(this, 60*1000);
// Show Ad
showInterstitial();
}
};
then...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the InterstitialAd and set the adUnitId
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
#Override
protected void onStart() {
super.onStart();
// Start showing Ad in every 60 seconds
//when activity is visible to the user
mHandler = new Handler();
//mHandler.post(mRunnable);
// Run first add after 20 seconds
mHandler.postDelayed(mRunnable,20*1000);
}
protected void onStop() {
super.onStop();
// Stop showing Ad when activity is not visible anymore
mHandler.removeCallbacks(mRunnable);
}
private void loadInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.setRequestAgent("android_studio:ad_template").build();
mInterstitialAd.loadAd(adRequest);
}
private InterstitialAd newInterstitialAd() {
InterstitialAd interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
mNextLevelButton.setEnabled(true);
}
#Override
public void onAdFailedToLoad(int errorCode) {
mNextLevelButton.setEnabled(true);
}
#Override
public void onAdClosed() {
// Reload ad so it can be ready to be show to the user next time
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
});
return interstitialAd;
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and reload the ad.
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
}
Make a new thread, then add a sleep time of 60000 milliseconds before showing your ad back. It will allow for async working.
Example, add this code in the onAdClosed() :
new Thread(new Runnable(){
public void run(){
Thread.sleep(60000);
// Show your ad here
}
}).start();

Add an interstitial admob ads without button pressed

I want to add an interstitial ads by admob without press any button but
it does not appear can you help I tried below codes:
...
public class MainActivity extends ActionBarActivity {
InterstitialAd mInterstitialAd;
Button mNewGameButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
beginPlayingGame();
}
});
requestNewInterstitial();
mNewGameButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}
}
});
beginPlayingGame();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
mInterstitialAd.loadAd(adRequest);
}
private void beginPlayingGame() {
// Play for a while, then display the New Game Button
}
}
...
While loading an add you must add one device id that you are using for testing . while u r running this code see ur logcat to see the device id .. Copy and paste that device id into add test device and rerun u will start seeing add
public class MainActivity extends ActionBarActivity {
InterstitialAd mInterstitialAd;
Button mNewGameButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEViCE_ID").build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
beginPlayingGame();
}
});
requestNewInterstitial();
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}
beginPlayingGame();
}
the true answer here i found it
interstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
MainActivity.this.interstitialAd.show();
}
}, 5000);
}} );

How to preload interstitial ads - android studio?

I created a small puzzle app, on completion of each level I wanted to display interstitial ads, but it is taking few seconds to load ads. So soon after the button is clicked it navigates to the next activity without showing ads. If I pause for few seconds and press the button it is showing ads. please help me out!
public class Completed extends AppCompatActivity {
InterstitialAd mInterstitialAd;
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Do you want to go to Home?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//if user pressed "yes", then he is allowed to exit from application
finish();
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//if user select "No", just cancel this dialog and continue with app
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
//AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_completed);
requestNewInterstitial();
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxxxxxx");
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
}
});
//requestNewInterstitial();
Typeface MyTypeFace=Typeface.createFromAsset(getAssets(),"comic.ttf");
Button btn=(Button)findViewById(R.id.btncomplete);
TextView txt=(TextView)findViewById(R.id.txtcomplete);
btn.setTypeface(MyTypeFace);
txt.setTypeface(MyTypeFace);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (MainActivity.setsound) {
MainActivity.mp.start();
}
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
finish();
}
});
}
}
on app start call init method:
public void init()
{
mInterstitialAd = new InterstitialAd(baseAppController);
mInterstitialAd.setAdUnitId(baseAppController.getString(R.string.interstitial_ad_unit_id));
mInterstitialAd.setAdListener(new AdListener()
{
#Override
public void onAdClosed()
{
requestNewInterstitial();
}
});
requestNewInterstitial();
}
private void requestNewInterstitial()
{
mInterstitialAd.loadAd(getAdRequest());
}
public AdRequest getAdRequest()
{
AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
return adRequest;
}
when you want to show ad just call below method:
public void showInterstitial()
{
if (mInterstitialAd.isLoaded())
{
mInterstitialAd.show();
}
}
use
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
}
public void onAdLoaded (){
//... your code is here
}
});

Admob After Splash Screen

Google changed admob using. Its allowing after splash screen interstial ad.
I write a code. Its working another app but a app. Its not working.
Its my Activity splash. I am using for splash. Firstly opening splash then admob interstial. Its working another app but this app not working.
public class ActivitySplash extends Activity {
InterstitialAd mInterstitialAd;
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// Prepare the Interstitial Ad
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-xxxxxxxx/xxxxx");
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
Intent i = new Intent(ActivitySplash.this, MainActivity.class);
startActivity(i);
}
});
requestNewInterstitial();
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
// Prepare an Interstitial Ad Listener
Intent i = new Intent(ActivitySplash.this, MainActivity.class);
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
startActivity(i);
}
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("C56E5F8DEC56E64E3997ED0856A63292")
.build();
mInterstitialAd.loadAd(adRequest);
}
A SplashActivity is a really poor place to put an interstitial. It is very likely that the interstitial will never be received before your app has loaded.
Adding interstitial add after the splash screen is not recommended as it annoys users. but If you want to display add then:
You have to add additional delay for add to load
when the ad is loaded, you can display it before launching the next activity.
bellow is working code(but this is not a good practice).
InterstitialAd ad;
ad = new InterstitialAd(this);
ad.setAdUnitId(getString(R.string.interstitial));
ad.loadAd(new AdRequest.Builder().build());
ad.setAdListener(new AdListener(){
#Override
public void onAdClosed() {
super.onAdClosed();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
});
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(ad.isLoaded()) {
ad.show();
}
else{
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
}
}, 4000);
As I have intentionally added the delay of 4 seconds (As a timestamp for the ad to load.).

Admob Interstitial ad is not clickable in android app exit

I am trying to show admob interstitial ad at the end of the application when user presses back button from the main activity. Ad is shown, but the ad is not clickable. My code is
I put loading part at the activity onCreate :
interstitial = new InterstitialAd(getApplicationContext());
interstitial.setAdUnitId("Ad UNIT ID");
// Create ad request.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
and in OnBackPressed :
#Override
public void onBackPressed() {
super.onBackPressed();
displayInterstitial();
}
protected void displayInterstitial() {
Log.d(TAG, "start displayInterstitial()");
if (null != interstitial && interstitial.isLoaded()) {
Log.d(TAG, "displayInterstitial() loaded");
interstitial.show();
}
Log.d(TAG, "end displayInterstitial()");
}
Can anyone please help me to fix this issue?
Simplest way would be:
interstitial.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
finish();
}
});
However, in my opinion it is better to stay at your app for a sec before app finishes. That way it is more clear that this ad was still part of your app:
interstitial.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
finish();
}
}, 1000);
}
});
BTW: about the comment of calling MainActivity.super.onBackPressed(); inside the adlistener's onAdClosed: I tested it and threw an IllegalStateException. I guess that is because the moment when onBackPressed is called is too late already. But well it is anyway not needed, so forget about it. The super onBackPressed method itself is accessible from the AdListener though and will compile just fine ;-)
#Override
public void onBackPressed() {
Log.d(TAG, "onBackPressed Called");
displayInterstitial();
//finish();
super.onBackPressed();
}

Categories

Resources