How to add Adsmob - Android

Finally got ads on my apps, I thought it's going to be hard but the documentation is so easy to understand, just go to documentation first if stuck go to my notes. I think I had a trouble, it says that my campaign is still running on adsword so I had to fix that by removing the campaign then it would let me register for admob. After registering it takes 2 days to be verified.

Dashboard -

update :

Little bit of tutorial just the main point, tried banner ads, rewarded ads but didn't get to try interstitial ads (on my future task)

implementation ''
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
# value string
<string name="ad_unit_id">ca-app-pub-3940256/6300</string>

Initialize after on create

MobileAds.initialize(this, new OnInitializationCompleteListener() {
            public void onInitializationComplete(InitializationStatus initializationStatus) {

create the view in here I use the responsive ads

        android:layout_alignParentBottom="true" />

In on create after initialize

	adContainerView = findViewById(;
        // Step 1 - Create an AdView and set the ad unit ID on it.
        adView = new AdView(this);

then make private function in the activity

private void loadBanner() {
        // Create an ad request. Check your logcat output for the hashed device ID
        // to get test ads on a physical device, e.g.,
        // "Use AdRequest.Builder.addTestDevice("ABCDE0123") to get test ads on this
        // device."
        AdRequest adRequest =
                new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR)

        AdSize adSize = getAdSize();
        // Step 4 - Set the adaptive ad size on the ad view.

        // Step 5 - Start loading the ad in the background.

    private AdSize getAdSize() {
        // Step 2 - Determine the screen width (less decorations) to use for the ad width.
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();

        float widthPixels = outMetrics.widthPixels;
        float density = outMetrics.density;

        int adWidth = (int) (widthPixels / density);

        // Step 3 - Get adaptive ad size and return for setting on the ad view.
        return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);


Also I tried the rewarded ads, this is when you want the user willingly open your ads in exchange of maybe reward.


rewardedAd = new RewardedAd(this,

        RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
            public void onRewardedAdLoaded() {
                // Ad successfully loaded.

            public void onRewardedAdFailedToLoad(int errorCode) {
                // Ad failed to load.
        rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);

        button_support.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (rewardedAd.isLoaded()) {
                    Activity activityContext = DetailActivity.this;
                    RewardedAdCallback adCallback = new RewardedAdCallback() {
                        public void onRewardedAdOpened() {
                            // Ad opened.

                        public void onRewardedAdClosed() {
                            // Ad closed.

                        public void onUserEarnedReward(@NonNull RewardItem reward) {
                            // User earned reward.

                        public void onRewardedAdFailedToShow(int errorCode) {
                            // Ad failed to display.
          , adCallback);
                } else {
                    Log.d("TAG", "The rewarded ad wasn't loaded yet.");


        android:text="Support Us"
        android:layout_height="wrap_content" />

Subscribe to You Live What You Learn

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]