I am working on an android app and I want to display interstitial ad every time a button in clicked. I have searched for it on stackoverflow and used the given suggestion but still I am not able to do it.
First I have declared interstitial ad as
private InterstitialAd mInterstitialAd;
private AdRequest adRequest;
I have used this:
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
adRequest = new AdRequest.Builder()
.build();
mInterstitialAd.loadAd(adRequest);
btn = (Button) findViewById(R.id.Chm);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
mInterstitialAd.loadAd(adRequest);
Intent i = new Intent(MainActivity.this, Btn.class);
startActivity(i);
}
}
);
But when I run the app, I click the above button I do not get any ad but when I click the back button after clicking the above button, I get the ad but that too only first time, not always. Please tell me my mistake.
Thanks.
As per my knowledge you can fix it by including setAdListner
mInterstital.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
mInterstital.loadAd(new AdRequest.Builder().build());
}
});
Related
I try to add an Interstitial ads, the problem is that i have 2 code to change.
The 2 codes to change in my MainActivity:
But in my admob account, I can only find the ca-app-pub-xxxxx....xx code and I can't find the ID_INTERSTITIAL_UNIT_ID.
Can any body please show me where to find the second one?
I tried to put the same links in the two code but I am afraid of being banned from admob.
You will need only <string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string>
and do this on you onCreate() method
//region Interstitial
String interstitialAdId=getResources().getString(R.string.interstitial_ad_unit_id);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(interstitialAdId);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed()
{
}
});
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
//endregion
then add
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
where you want to start the Interstitial
I need some help the problem is that when Interstitial ads open and when it closes it unloads the loaded ad from the variable I am using for displaying Interstitial ADS , my question is can I stop it from unloading the ads ? If yes then how can I do it?
This is normal.
Same add can be displayed only once. After it is closed, you must request a new one. You can use same InterstitialAd object to request a new AD. However, you really must load a new one.
You can create an AdListener and check when the add is closed. This way, you can request a new one.
In AdMob's DOC page, you can find some examples:
public class MainActivity extends ActionBarActivity {
InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("AD UNIT ID");
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
}
});
requestNewInterstitial();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
mInterstitialAd.loadAd(adRequest);
}
}
I am implementing admob ads in android but Interstitial ads are not showing and only the banner shows.
is it required for the Banner Ads and the Interstitial Ads to have different Ad Unit ID in Admob Android?
Any suggestions
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("myAdUnitId");
// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
// Set an AdListener.
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Toast.makeText(MyActivity.this,
"The interstitial is loaded", Toast.LENGTH_SHORT).show();
}
#Override
public void onAdClosed() {
// Proceed to the next level.
goToNextLevel();
}
});
// Start loading the ad now so that it is ready by the time the user is ready to go to
// the next level.
mInterstitialAd.loadAd(adRequestBuilder.build());
// Create the button to go to the next level.
mNextLevelButton = new Button(this);
mNextLevelButton.setText("Next Level");
mNextLevelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Show the interstitial if it is ready. Otherwise, proceed to the next level
// without ever showing it.
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
// Proceed to the next level.
goToNextLevel();
}
}
});
// Add the next level button to the layout.
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(mNextLevelButton);
// Create a TextView to display the current level.
mTextView = new TextView(this);
mTextView.setText("Level 1");
layout.addView(mTextView);
setContentView(layout);
}
public void goToNextLevel() {
// Show the next level (and disable the next level button since there are no more levels.
mNextLevelButton.setEnabled(false);
mTextView.setText("Level 2");
}
}
Here is a nice example on developers.google.com
Check your AD Type id.Very it both in your admob account and source code that the AD Unit Id belong to interstitial Ad.
If thats correct use this code
public partial class MainPage : PhoneApplicationPage
{
private InterstitialAd interstitialAd;
// Constructor
public MainPage()
{
InitializeComponent();
interstitialAd = new InterstitialAd("your id");
AdRequest adReques = new AdRequest();
interstitialAd.ReceivedAd += OnAdReceived;
interstitialAd.LoadAd(adReques);
}
private void OnAdReceived(object sender, AdEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Ad received successfully");
interstitialAd.ShowAd();
}
i want to have an admob interstitial ad show up when the user first opens the app or when the user navigates to another app (like browser) and then returns to my app. This is my current code for interstital ad, this code is entirely contained inside the OnCreate method.
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-XXXXXXXXXXXXXXXX");
// Create ad request.
AdRequest adRequest2 = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest2);
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});
This seems to work for most situations, but on certain devices this will create a loop of interstitial showing 2-3 sec after they are dismissed by the user. one of those device that has the loop is a Galaxy Tab3 . i cant seem to figure out a proper way to setup my code so that this behavior does not happen on any device.
Use the following code to display the interstitial ad when appstarts.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("***********");
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
}
});
}
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
Create a boolean var to assist with it..
boolean ad_shown = false;
When you do .show() turn the variable to true.
Don't forget to put a guard on the if
if (interstitial.isLoaded() && !ad_shown) {
i wanted to call the Admob interstitials? multiple time in my Android app. i am tottally confusing this things.
interstitial = new InterstitialAd(this, "ID");
final AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
interstitial.setAdListener(this);
i am tried this code this works fine for one time call. when it will call second time show me error.
help me out this.
thanks in advance.
You need to separate out construction of the Interstitial, loading the ad and showing the ad.
public void onCreate(Bundle savedInstanceState) {
interstitial = new InterstitialAd(this, "MY-AD-ID");
interstitial.setAdListener(new AdListsner() {
..
});
}
private void loadAd() {
final AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
private void showAd() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}