Monday, 30 July 2018

How to Integrate Google Banner AdMob in your App

AdMob is a platform mobile ad network that allows you to monetize your android app. By integrating Admob you can earn some money from your free app.

Integrating AdMob is such an easy task. FirstBannerAds app with one screen to show the Banner ads that AdMob supports.


A.) Create AdMob Account

Step-1. Sign into your AdMob account.
Step-2. Create a new App by giving the package name of the app you want to integrate AdMob.
Step-3. Select App and create a new ad unit.
Step-4. Select the Banner ad format and give the ad unit a name.
Step-5. Once the ad unit is created, copy both id and paste your android project/app:-
             App Id: ca-app-pub-XXXXXXXXX~XXXXXXXXX.
             Ad Unit Id: ca-app-pub-066XXXXXXX/XXXXXXXXXXX

See full android interview questions: Android Interview Questions

B.) Create New Project

Step-1. Create a new project in Android Studio
             File -> New Project -> Empty Activity.

Step-2. Open build.gradle(Project: FirstBannerAds) and add below code.

  allprojects {
     repositories {
         google()
         jcenter()
         maven {
            url "https://maven.google.com"
           }
       }
  }

Step-3. Open build.gradle(Module: App) and add play services dependency and click sync now.

 implementation 'com.android.support:appcompat-v7:26.1.0'
 implementation 'com.android.support:cardview-v7:26.1.0'
 implementation 'com.google.android.gms:play-services-ads:15.0.1'
 // play services dependency as AdMob requires it.

Step-4. Add below required uses-permission in AndroidManifest.xml.

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

Step-4. Add below code in activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
   android:layout_height="match_parent" 
   android:layout_width="match_parent"
    android:padding="12dp" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

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

</RelativeLayout>


*Note: Ad Unit Id: ca-app-pub-3940256099942544/6300978111 testing AdMob. Be assure, before publish app on google play store change this Ad Unit Id to your own unit id.

Step-4. Add below code in MainActivity.java


package com.example.firstbannerads;

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

import com.google.android.gms.ads.MobileAds;
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);

        MobileAds.initialize(this, "ca-app-pub-XXXXXXXXX~XXXXXXXXX");

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

    }
}

Step-4. Now run the app.




See full android interview questions: Android Interview Questions





No comments:

Post a Comment