admob ads

How to add an AdMob banner in your android app?

You successfully make your first app in android, next is to put ads on it for you to generate income. To do so, you need to register in Google AdMob. After registering, you can now start to create an ads.

How to add an AdMob banner in your android app?

1.First thing you need to do is click the Apps on the left side of the screen under Home. Select the add new app button, and answer the question of whether you already published your app in google play. If not, click on No.

2. Next is to enter your app name, and the platform, either an android or ios. Tick the enabled metrics for this app, and finally, click add.

3. Third step is to make your add unit. Instruction will be given on how to do so. Click “create add unit”.

4. You need to choose from different types of ad units including the banner, interstitial, rewarded, native advanced, or the App Open.

Banner Ads- Rectangular ads that occupy a portion of an app’s layout; can be refreshed automatically after a period of time

Interstitial- Full-page ad format that appears at natural breaks and transitions, such as level completion.

Rewarded- Ad format that rewards users for watching ads. Great for monetizing free-to-play users.

Native advanced- Customizable ad format that matches the look and feel of your app; appears inline with app content.

App openBETA– Ad format that appears when users open or switch back to your app. Ad overlays loading screen.

Since the most common ad is the admob banner, we will be focusing on this one.

5. Select the Banner Ads, and enter an ad unit name. Click the create AD Unit. Next is to place the ad unit in your app by following the instruction below.

Google Mobile Ads SDK

You need to check first on your build.gradle that google() is referenced in the allprojects section of your project-level build.gradle file.

allprojects {
    repositories {
        google()
    }
}

You can check this by clicking the Gradle script on the left side the select the first build.gradle (Project)

Next is to open the second build.gradle (Module app) and look for the dependencies.

Add the code implementation ‘com.google.android.gms:play-services-ads:19.5.0’ on the dependencies section after the implementation ‘androidx.appcompat:appcompat:1.0.2’. as shown below.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.gms:play-services-ads:19.5.0'
}

Go to the Android manifest.xml and add a <meta-data> tag with name com.google.android.gms.ads.APPLICATION_ID, as shown below.

<meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>

For the android value: change it with your APP ID.

Initializing Mobile Ads SDK

Now initialize the mobile ads SDK by adding the  MobileAds.initialize()in the Main Activity.

public class MainActivity extends AppCompatActivity {

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

Type below this code: MobileAds.initialize(this, new- it will show the complete code, then just press enter.

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

            }
        });

By doing this, it will automatiically input as well this code on import part.

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

You can now put the Banner code. First is to put an Adview to your layout. Go to main_activity.xml, you by clicking Res then lay-out. Copy and paste this code:

<com.google.android.gms.ads.AdView
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:id="@+id/adView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_alignParentBottom="true"
      ads:adSize="BANNER"
      ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
  </com.google.android.gms.ads.AdView>

Replace the Ad unit id with your own unid ID.

Then load your ad by adding this code under the Mobile.Ads initialize:

 private AdView mAdView;

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

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

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

First is to type the private Advie mAdview below the

public class MainActivity extends AppCompatActivity {
then one by one type this:
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}

Thats it. Your banner Ads is ready. Generate signed APK and upload it in Google Play Store.