RichFounders RichFounders - 3 months ago 17
Android Question

Set Display Ads when button clicked

I have a little question here. How can I make ads show up when the button is clicked?

Okay I will explain with my

menu.class
here

public class menu extends Activity {

private InterstitialAd mInterstitialAd;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);

// Prepare the Interstitial Ad
mInterstitialAd = new InterstitialAd(menu.this);

// Insert the Ad Unit ID
mInterstitialAd.setAdUnitId("ca-app-pub-3502149848009990/xxxxxxxxx");

// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();

// Begin loading your interstitial.
mInterstitialAd.loadAd(adRequest);

// Prepare an Interstitial Ad Listener
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});

Button play = (Button)findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
Intent i =new Intent(getApplicationContext(), aselectmenu.class);
startActivity(i);
}
});

Button exit = (Button) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
final Dialog openDialog = new Dialog(menu.this);
openDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
openDialog.setContentView(R.layout.inflatequitapp);

TextView dialogTextContent = (TextView)openDialog.findViewById(R.id.exitimage);

Button dialogExitButton = (Button)openDialog.findViewById(R.id.buttonexityes);
dialogExitButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
});
Button dialogCloseButton = (Button)openDialog.findViewById(R.id.buttonexitno);
dialogCloseButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
openDialog.dismiss();
}
});
openDialog.show();
}
});
}

public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
@Override
public void onBackPressed() {
// do nothing.
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


my question is in this line

Button exit = (Button) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
final Dialog openDialog = new Dialog(amenu.this);
openDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
openDialog.setContentView(R.layout.inflatequitapp);

TextView dialogTextContent = (TextView)openDialog.findViewById(R.id.exitimage);

Button dialogExitButton = (Button)openDialog.findViewById(R.id.buttonexityes);
dialogExitButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
});
Button dialogCloseButton = (Button)openDialog.findViewById(R.id.buttonexitno);
dialogCloseButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
openDialog.dismiss();
}
});
openDialog.show();
}
});
}


How to put
displayInterstitial()
method in there so when button clicked the ads will show up?

Answer

Call the displayInterstitial() method when before finish():

dialogExitButton.setOnClickListener(new OnClickListener(){                                      
               @Override
               public void onClick(View v) {
                  displayInterstitial();
                  finish();
                  } 
            });

However, you should note that according to the disallowed admob 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.

Failure to adhere to this policy might lead to the disabling of ad-serving to your app.

So, to stay on the safe side show the ad during a transition from one screen to the other, preferably in the play button's event handler:

  Button play = (Button)findViewById(R.id.play);           
  play.setOnClickListener(new View.OnClickListener() {

                 @Override
                 public void onClick(View view) {
                       Intent i =new Intent(getApplicationContext(), aselectmenu.class);
                       displayInterstitial();
                       startActivity(i);
                     }               
             });