Find your content:

Search form

Learn Admob integration with Android SDK - Layout and Java Changes


Layout Changes

Add Adview in your layout and make sure in designer you provided proper design for this adview. In design layout, if it is not showing up the adview block then it won't visible in actual app screen as well. 



Java Changes

The tricky part of this change is below line. Below line works fine in simulator and production. In simulator you see testad and in production you could see live ad. 

AdRequest adRequest = new AdRequest.Builder().addKeyword("game").build(); 

addKeyword is optional earlier I tried without addKeyword() I didn't get any ads so I added. You could add any keyword instead of game. You have to check without addKeyword and wait for few hours to see test ad if it is not coming add some keywords and try.


If you want to test your changes with your device you have to use the below line and make sure only test ads will be displayed in your mobile. Even if you sign the apk and test with your device it won't show real ad(for my case it didn't but I am not sure)

  AdRequest adRequest = new AdRequest.Builder().addTestDevice("<<DEVICEID>>").build(); 



public class MyActivity extends AppCompatActivity{
	private AdView mAdView;
    public void onCreate(Bundle savedInstanceState) {
		try {
                final MyActivity activity = this;
                MobileAds.initialize(ctx, new OnInitializationCompleteListener() {
            public void onInitializationComplete(InitializationStatus initializationStatus) {
                 AdView  mAdViewFinal = (AdView) activity.findViewById(;
                 AdRequest adRequest = new AdRequest.Builder().addKeyword("game").build(); 
                 mAdViewFinal.setAdListener(new CustomAdListener());



AdListener implementation (it is pretty standard one)


private class CustomAdListener extends AdListener{

        public void onAdLoaded() {
            System.out.println("ad onAdLoaded:");
            // Code to be executed when an ad finishes loading.

        public void onAdFailedToLoad(int errorCode) {
            System.out.println("ad onAdFailedToLoad:" + errorCode);
            // Code to be executed when an ad request fails.

        public void onAdOpened() {
            System.out.println("ad onAdOpened:");
            // Code to be executed when an ad opens an overlay that
            // covers the screen.

        public void onAdClicked() {

            // Code to be executed when the user clicks on an ad.

        public void onAdLeftApplication() {
            // Code to be executed when the user has left the app.

            public void onAdClosed() {
            // Code to be executed when the user is about to return
            // to the app after tapping on an ad.



Important Points

Test your implementation with simulator first

AdRequest adRequest = new AdRequest.Builder().build();

or below one 

AdRequest adRequest = new AdRequest.Builder().addKeyword("game").build(); 

should work. You have to be patient for 1 or 2 hours to see the test ad). Once you see the test ad then change the code to test the ad in real device by adding device id in adrequest.

AdRequest adRequest = new AdRequest.Builder().addTestDevice("<<DEVICEID>>").build();

When you are doing testing in device, checkout the logs on CustomAdListener and give some time to display the test ads(may be 1hr)


If test ads are displaying in simulator and device then we are good now. Next step is how to handle onResume() and onDestroy() methods. Lets check on this next





My Block Status

My Block Content