How to Add Admob ADS in your Android App

How to put admob ads in Android

Admob is a mobile advertisements platform run by google. admob is better than any other ad networks because the payment and quality is good. they are the most trusted ad network and millions of advertisers use admob.

We will learn how to add/integrate Google Admob ads into your android app and make money from your app users by showing ads to them In this tutorial.

 

Types of Admob Ads

  1. Banner Ads – (Basic Ads,This type of ads placed in the bottom of your app)
  2. Interstitial Ads – (Full screen ads)
  3. Native Ads – (You can customize this type of ads to match the look of your app)
  4. Rewarded Video – (your user can watch this video ads if you want to let them use your app or game free)

in this tutorial, we will try to add a banner ad into our app.

Steps to show ads in your app

  1. Sign Up for Google Admob Account
  2. Register your app and create an Adunit
  3. Add the SDK
  4. Add some codes in your app in android studio

Sign Up on Admob for an account, Sign Up for Admob here.

after sign up, Create a Banner ad format, get your app id and get your unique adunit id for your app. then let’s continue to next step

Add the SDK

In this step, we are going to add the ad sdk to your app. after adding the sdk and a few lines of code, your app will be able to show ads.

Open your android studio and in the navigation menu, select Tools > Firebase. a new menu will appear

 

In bottom of the menu, you can see “Admob” , select “add a banner ad to your app“.

Select “Connect to firebase“. now the android studio will try to connect to firebase, you will need log in to your gmail account, you might need logged in or sign up into firebase also. wait until the android studio successfully connect to firebase.

 

After connection, a new menu will appear asking you to add your app to firebase. enter your app name into the text box and select your country. click “Connect to firebase” button. and wait few minutes until android studio connect your app to firebase and do some work in the background.

 

Now the android studio will ask you to add the SDK to your project gradle files, this adds the ads sdk library to your app. this library is required to load and show ads in your app.

Next click on the Add admob to your app button, a new menu will appear asking your permission to add the dependencies .

Click “Accept changes” and you need to wait until android studio sync the library with gradle files and add the required dependencies.

Now your app is almost ready to show ads. in next steps, we will need to add some xml and java codes into our app.

Adding codes to layout and java

This is the final step, if all of the above steps have completed, let’s continue

Open your activity_main.xml layout file, or other layout where you want to add banner ad. in that layout, add the following code in the top.

xmlns:ads="http://schemas.android.com/apk/res-auto"

 

Next, add this code, this code places ad banner view in the bottom of your app layout.

< android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:alpha="0.9"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/"/>

 

Screenshot for reference

 

you can see red in “ads:adUnitId=”@string/banner_ad_unit_id“>”. the red indicates error. we need add the adunit id

in the Strings.xml.  you need your adUnit Id from admob. and go to res > values > strings.xml.


 

In the strings.xml, add this adunit id code.

ca-app-pub-3940256099942544/6300978111

Screenshot

Replace the “ca-app-pub-3940256099942544/6300978111” with your adunit id later. this current id is for testing ads. keep in mind that you should always use test adunit id. testing your app with your real adunit id is against admob policy and they can suspend your admob account. you should use your real adunit id only when releasing the app.

 

So we added sdk, added codes in the layout file, added adunit id in the strings. next we need add some codes to the java.

Open your MainActivity.java or the activity where you wish to add the ads. add the codes to the java, this is full code, your java code should look like this.

package com.example.myapp.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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


public class MainActivity extends AppCompatActivity {

    private AdView mAdView;

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

        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

}}

 

Screenshot of the java code in android studio

This is it.

We have completed all the steps. now your app should be ready, we can test it now. remember to test it with test adunit id only

 

Testing

The app is now launched and a test ad is shown in the bottom of your screen. this is a test ad, by replacing the adunit id in strings.xml with your unique ad unit id, the real ads will be shown.

If you need more help, you can use the comments below

, , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.