Search code examples
javaandroidbanner-ads

Not display banner ad in the drawable canvas in android


I implement the game through drawable canvas in android and i want the banner ads but its unable to display. Here is the code :-

GameActivity.java (This GameActivity.java is connected to my gameview.java file where i use the all game code with the help of drawable canvas)

public class GameActivity extends Activity
{
    private AdView mAdView;

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {

    super.onCreate(savedInstanceState);
    //GameView gView = new GameView(this);
    GameView gView = new GameView(this);
     gView.setKeepScreenOn(true);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(
     WindowManager.LayoutParams.FLAG_FULLSCREEN,
     WindowManager.LayoutParams.FLAG_FULLSCREEN);
     MobileAds.initialize(getApplicationContext(), "ca-app-pub-0664573200302260/332653322424");
     LinearLayout layout = new LinearLayout(this);
     layout.setOrientation(LinearLayout.VERTICAL);

//Additionally to adjust the position to Bottom
     layout.setGravity(Gravity.BOTTOM);

// Create a banner ad
     mAdView = new AdView(this);
     mAdView.setVisibility(View.VISIBLE);
     mAdView.setAdSize(AdSize.BANNER);
     mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

// Create an ad request.
     AdRequest.Builder adRequestBuilder = new AdRequest.Builder();


// Optionally populate the ad request builder.
     adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

// Add the AdView to the view hierarchy.
     layout.addView(mAdView);

// Start loading the ad.
     mAdView.loadAd(adRequestBuilder.build());



     setContentView(gView);
 }
}

Solution

  • You set the content view to GameView. Set it to the layout instead:

    setContentView(layout);
    

    GameView is a single view, while the layout contains both GameView and the AdView