Google Admob is a platform that can be monetised an app through Google Ads. You will be shown how to integrate Google Admob SDK to an application step by step.
First thing which is needed to do is creating an ad unit in Google Admob dashboard. To create an ad unit , you need to sign in to your AdMob account at https://apps.admob.com. After signing in , click Monitize tab then click +Monitise New App tab at Admob dashboard.
You will be redirect to a form. Click on Add your app manually tab. Then fill App Name field and choose the platform.
Click Add app button for creating Ad Unit.
An ad unit can be define as model of each ad. An Ad Unit ID is given after it is created. Then ads which will be shown be called with this IDs from app.
Following details needs to be provided in this screen ;
Ad Type : Choose which type of ads are wanted to be shown in Ad unit.
Automatic refresh: Refreshment option of ad. It can be completely disable or set up to 120 seconds. Recommended refresh rate is from 45 to 60 seconds.
Text ad style: Ad text style can be customised through this option.
Ad unit name : A unique name needs to be assigned to the ad unit.
After providing the details you will be given an Ad Unit ID after clicking Save button.
It is needed to integrate Google Mobile Ads SDK as dependency for showing ads in app .
Open app's build.gradle file and add this code in dependencies section. Then click Sync to synchronise the project.
compile 'com.google.android.gms:play-services-ads:11.8.0'
This code needs to be added in project-level build.gradle file.
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
Mobile Ads SDK has to be initialised with Admob App ID before loading ads. It only needs to be done once. So, ideally it can be at app launch.
Here's an example of how to call the initialize() method in an Activity:
public class MainActivity extends AppCompatActivity {
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
}
...
}
The first step to show a banner ad is to place an AdView to the layout of Activity or Fragment which is shown .
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
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="YOUR_AD_UNIT_ID">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
Note : If more than one banner will be shown in different activities or fragments , It is necessary to define an Ad Unit for each.
AdView can be created programmatically as well :
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("YOUR_AD_UNIT_ID");
// TODO: Add adView to your view hierarchy.
Note : It is better be tested with test Ad Unit ID : ca-app-pub-3940256099942544/6300978111
Loading an ad is done with the loadAd() method in the AdView class. It takes an AdRequest parameter, which holds runtime information (such as targeting info) about a single ad request.
import com.google.android.gms.ads.AdView;
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this,
"ca-app-pub-3940256099942544~3347511713");
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
The ad is ready. It will be shown when the app is run.