I have a layout with four buttons. When the user clicks on one of the buttons, I want to show interstitial ads .
My code below works when I click on one of the four buttons, but when I click again, the interstitial ads don't show.
For example:
BUTTON1BUTTON2BUTTON3BUTTON4
When I click for the first time on BUTTON1 the interstitial ads show; when I want to click again on one of the buttons (BUTTON2, for example), the interstitial ads are not showing at all.
How can I get the interstitial ads to show each time I click the button?
InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitialAd));
mInterstitialAd.loadAd(adRequest);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
//button 1 function
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
//button 2 function
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
//button 3 function
}
});
btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
//button 4 function
}
});
After displaying an InterstitialAd, you should request a new one with a new AdRequest:
So, one option is add following lines to your every ClickListener (not discussing how to improve the quality... just how to fix it):
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
mInterstitialAd.loadRequest(new AdRequest.Builder().build());
}
Could you please test and share the results (you may need to wait until a new AD is loaded before clicking on next button).
Related
I'm trying to integrate Interstitial Ad in splash screen activity by using this tutorial .. but the ad not loading.
can anyone tell me where is the problem please?
thanks in advance
here is my code :
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
mInterstitialAd.setAdListener(new AdListener()
{
#Override
public void onAdLoaded() {
if (!interstitialCanceled) {
waitTimer.cancel();
mInterstitialAd.show();
}
}
#Override
public void onAdFailedToLoad(int errorCode) {
startHomeMain();
}
});
waitTimer = new Timer();
waitTimer.schedule(new TimerTask() {
#Override
public void run() {
interstitialCanceled = true;
SplashScreenActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
startHomeMain();
}
});
}
}, 5000);
} // end of onCreate implementation.
I'm trying to load interstitial ad on the tablet, with the test ad id. On output ad is not showing, but I'm getting the callback on "onAdLoaded".
This is my code:-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ad_activity);
mPublisherInterstitialAd = new PublisherInterstitialAd(this);
mPublisherInterstitialAd.setAdUnitId("/6499/example/interstitial");
mPublisherInterstitialAd.loadAd(newPublisherAdRequest.Builder().build());
mPublisherInterstitialAd.setAdListener(new AdListener(){
#Override
public void onAdLoaded() {
Log.d("AD ","LOADED");
}
#Override
public void onAdFailedToLoad(int i) {
Log.d("AD ","FAILED");
}
});
}
Try this
public void inrequestadd() {
mInterstitial = new InterstitialAd(MainActivity.this);
mInterstitial.setAdUnitId("Your ID");
mInterstitial.loadAd(new AdRequest.Builder().build());
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
super.onAdClosed();
}
#Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
}
#Override
public void onAdLoaded() {
super.onAdLoaded();
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
}
});
}
Here is how I handle this kind of code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initAds();
}
private void initAds() {
MobileAds.initialize(this, "your app id");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("your ad unit ID");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Load the next interstitial.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
}
Also in the manifest file:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="your app id"/>
Next, depending on the logic of your app, you would have something like:
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
System.out.println("The interstitial wasn't loaded yet. Do some other action");
}
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);
}} );
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
}
});
I want to add an interstitial ad on my game just before the game starts.
When you run the app, you have 3 diferent game modes so when you select one, the game starts on that mode. The idea is to show the interstitial after you press the button, just before the game starts.
I have followed the guide on the android developers page, here, but this doesn't fit exactly what I need. I have modified a bit to fit with my code, but the interstitial is not shown before starting the game, it is shown when you finish the game and returning to mainActivity.
This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1Player = (ImageButton) findViewById(R.id.OnePlayerImgBtn);
btnVersus = (ImageButton) findViewById(R.id.VersusImgBtn);
btnLocalMultiP = (ImageButton) findViewById(R.id.LCLMultiPlayerImgBtn);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.ad_intersticial_1_id));
requestNewInterstitial();
btn1Player.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
startGame(false,true,MODE_SINGLE);
}
});
btnVersus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
startGame(false,true,MODE_VERSUS);
}
});
btnLocalMultiP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
startGame(true,true,MODE_LOCALMULTI);
}
});
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
}
});
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
public void displayInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
String singlemode ="Off";
String versus ="Off";
String multi ="Off";
btn1Player.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
String singlemode ="On";
}
});
btnVersus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
String versus ="On";
}
});
btnLocalMultiP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayInterstitial();
String multi="On";
}
});
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
if singlemode.equals("On");
{startGame(false,true,MODE_SINGLE);}
if versus.equals("On");
{startGame(false,true,MODE_VERSUS);}
if multimode.equals("On");
{startGame(true,true,MODE_LOCALMULTI);}
requestNewInterstitial();
}
});