Find your content:

Search form

Learn Admob integration with Android SDK - Layout and Java Changes

 
Share

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. 

	<com.google.android.gms.ads.AdView
			android:id="@+id/adView1"
			android:layout_width="match_parent"
			android:layout_height="80dp"
			android:layout_alignParentBottom="true"
			android:layout_centerHorizontal="true"
			ads:adSize="BANNER"
			ads:adUnitId="@string/adunitid"></com.google.android.gms.ads.AdView>

 

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 {
       		super.onCreate(savedInstanceState);
                final MyActivity activity = this;
                MobileAds.initialize(ctx, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
                 AdView  mAdViewFinal = (AdView) activity.findViewById(R.id.adView1);
                 activity.setmAdView(mAdViewFinal);
                 AdRequest adRequest = new AdRequest.Builder().addKeyword("game").build(); 
                 mAdViewFinal.loadAd(adRequest);
                 mAdViewFinal.setAdListener(new CustomAdListener());

            }
        });
}

 

AdListener implementation (it is pretty standard one)

 

private class CustomAdListener extends AdListener{

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

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

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

        @Override
        public void onAdClicked() {

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

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

            @Override
            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