How To Implement Google Ads in Your Android App for Monetization - Hive Programmers

avatar

Greetings to my favorite science community online, StemSocial.

We're super close to the end of 2023 and the beginning of 2024. It's been an awesome year and I've been blessed joining the StemSocial community.

We started an awesome series called Android App development tutorials for beginners almost two months ago on StemSocial and I'm really grateful that we have come this far.

We've built a lot of basic Android Apps and sometimes Apps are not just built to provide a service to users or solve a problem but as a way to generate revenue or income for the developers.

Polish_20231230_152258577.jpgOriginal Image Source by Mohamed_hassan from Pixabay

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Developing Apps can literally turn into a full time work and demands a lot of time and effort. This is the more reason why App developers need to be supported financially from their Apps.

There are two main ways you can generate money from you App on the Google Play store.

You can either make your App a paid app and put a price on it which a user would have to pay to be able to download the App or you can decide to make the App free and implement mobile ads in the App.

There are a lot of different mobile ads services out there you can use but the most popular and most convenient to use as a developer is Google's own Admob.

Of course there are a lot of rules and requirements around using Admob ads in your App and you can find out more on that in their official website page.

For today's tutorial, I'm going to teach you how to implement Admob banner ads in your App.

You've probably seen a banner ad before because they were among the first type of ads used and are still quite popular.

Although nowadays, developers are using more advanced, convenient and user-friendly types of ads, banner ads are really cool to implement in your App.

Let's get started with today's work shall we.

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Prerequisites

Of course guys, before you can implement admob ads in your Android App, you need to have the necessary softwares required to build Android Apps in the first place.

You need to have Android Studio IDE installed on your computer and you also need Java Development Kit, JDK installed so that your PC can execute Java instructions.

Now the next thing you need to have before getting started with your App project is an Admob Account.

Setting Up AdMob Account

To create your admob account, you need to head over to the main Admob website and sign up ideally using your Google account.

They would probably require an email verification if you're a new user so go through all the necessary prompts and sign up to create an account or sign in if you've already signed up.

Below is a link to the main Admob website, you can go there to create your new Admob account.

Google AdMob Website

After you successfully log in, create a new AdMob ad unit for banners ads. There are different types of ads like interstitial ads, video ads and native ads to name a few.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Adding AdMob Dependency

Now guys you can open Android Studio and create a new project or open the existing project you intend to show the Admob banner ads in.

Since Admob ads is a library we intend to add to our app project, you're going to have to download the library files by adding its dependency to your build.gradle (Module: app) file

Here's how the dependency code should look like.

implementation 'com.google.android.gms:play-services-ads:20.5.0'

After adding the dependency, click on the sync button that appears at the top of your Android Studio project screen and the ads library will be downloaded and dependency synced with your entire project.

After this step is successful, you're ready to write the codes that would include Google Admob ads to your App.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Requesting Internet Permissions

Now guys we all know that ads run on internet service, it cannot show without your App having access to the Internet.

This would mean that we need to request internet permission inside our AndroidManifest.xml file.

Additionally we would also need our App to be able to access the network state of the Android device to be able to tell whether the device is online or offline to help handle errors.

Here's how the Permission request codes would look like

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Creating our AdMob Banner Ad layout

It's now time for the users interface design of our App. We need to have more like a placeholder for our AdMob banner ad.

We need to create a layout in our App where the banner ads will occupy.

In this situation we'll be implementing the banner ads inside our activity_main.xml but of course you can create an activity of your own and implement the banner ads there.

Here's how the Admob banner ads layout code should look like

<LinearLayout
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    (html comment removed:  Your existing layout here )

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="YOUR_ADMOB_BANNER_AD_UNIT_ID" />
</LinearLayout>

The ads:adSize="BANNER line of code determines the actual default dimensions of our App and so this ad will have the standard banners ads dimensions and size.

I would always recommend getting the default standard size or dimensions but you can totally customize it to any degree as long as you still follow the rules of Admob ads implementation.

Now guys don't forget to replace "YOUR_ADMOB_BANNER_AD_UNIT_ID" with the actual AdMob banner ad unit ID you obtained from the Google Admob website while you were creating your banner ads.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Initialize AdMob in MainActivity

It's time to write the backend code to initialize the banner ads and make it live in our App.

We'll be writing the backend code for that inside our MainActivity.java

We must start by initializing AdMob in the onCreate method after which we'll use the adRequest method to request ads and the adView.loadAd method to show the ads in our App if the ads are live.

Here's how the code should look like

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {

    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize AdMob
        MobileAds.initialize(this);

        // Load AdMob banner ad
        adView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
    }
}

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Test AdMob Ads

We're basically done with our ad implementation but during development, it's very crucial and highly recommended that you test the AdMob ads.

To do the testing, you would have to replace the actual ad unit ID with the test ad unit ID. If you don't use the test ad unit instead, you might actually end up getting banned by Admob for trying to abuse the ad network system.

However, currently Admob has created a new strategy where they only show real live ads in Apps that get a certain amount of traffic as a Proof of the App's legitimacy.

Here's how your ads test codes should look like

AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // Emulator
    .addTestDevice("YOUR_TEST_DEVICE_ID") // Your test device ID
    .build();

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Run Your App

Congratulations guys, you're done implementing admob banner ads in your App. After you build a great App, you can implement this and earn money from your App.

You can check the progress or incomes of your App Ads revenue from the Admob console page.

Click the green triangle rum button to build and run your app on a physical Android device or an emulator.

When the App launches, you should see AdMob banner ads displayed in the designated area.

Now guys, please remember to replace placeholder values with your actual AdMob ad unit IDs when you are ready to publish your app on the playstore or any other Android app store.

Thank you so much for taking the time to read today's blog. I hope you enjoyed it and found it both insightful and entertaining.

As I promised in my previous blog, another year is coming so you can definitely expect some fresh new episodes in the series; Android App Development Tutorials for beginners.

Have a Great day and catch you next time on StemSocial. Goodbye 👨‍💻


You Can Follow Me @skyehi For More Like This And Others



0
0
0.000
5 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support. 
 

0
0
0.000
avatar

Developers can earn in several ways, hopefully, I become an expert too and I will be able to launch my own developed app.

0
0
0.000
avatar

You can dear friend, it takes dedication and practice 🥰

0
0
0.000
avatar

you always looks simply and esay to do :D, great wotk and article

0
0
0.000
avatar

Thank you so much dear friend ❤️❤️ happy new year ❤️

0
0
0.000