Admob Banner and Interstitial Ads in Android Apps using Android Studio
Admob Banner and Interstitial Ads in Android Apps using Android Studio
In this article we will see that how to add Google Admob with banner and interstitial Ads in Android Apps using android Studio.
Please following all the step by step to implementation ads on your android application.
Step 1. First download google repository package using sdk manager in Android Studio.
Step 2. Once you done with downloading above package next update the app to reference the google play services ads sdk in build.gradle file as follows.
build.gradle :
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.android.gms:play-services-ads:17.2.0'
}
Now next after adding above google play service ads sdk in our build.gradle file you can observe alert is saying that sync now just go with it by select sync now option.
Step 3. Next thing is we need to modify the manifest file by adding permissions, meta data tag and activity tag as follows.
AndroidManifest.xml:
<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<application>
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="YOUR_ADMOB_APP_ID"/>
</application></manifest>
If you don't have admob account it is very simple just login to https://www.google.com/admob/ website by using your google user name and password.
Once you complete the admob setup by giving some information in the main dashboard you can see the option monetize new app just select it and next select add app your manually next give your app name and select platform android and next click add button in the next step select ad formats like banner or interstitial create id's and finally you can finish the setup by clicking done button.
Note : when ever your app in testing stage don't use your real admob id's instead use test ad id's those i'm providing in this tutorial , before submitting your app in play store at that time replace test ad id's with real ad id's check and publish app .
if your using real id's in testing stage your admob account can be suspended without giving intemation.
Step 4. Next create strings in strings.xml to refer admob banner,iterstial id's here i'm using test id's .
strings.xml:
<resources>
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string> <string name="interstial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string>
</resources>
Following code is for banner ad's:
Step 5. Next to show banner ad in any activity look at the code below in xml layout and as well as activity.
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
MainActivity.java:
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends AppCompatActivity {
//For Banner Ads
AdView mAdView
//For Interstital Ads
InterstitialAd mInterstitialAd;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
Following code is for interstial ad's:
MainActivity.java:
//To show banner id
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
//To show interstial ad
mInterstitialAd = new InterstitialAD(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstial_ad_unit_id));
AdRequest adRequest = new AdRequest.Builder().Builder().build();
mInterstitialAd.setAdlistener(new AdListerner(){
@Override
public void onAdLoaded(){
displayInterstitial();
}
});
}
public void displayInterstitial(){
if(mInterstitialAd.isLoaded()){
mInterstitialAd.show();
}
}
}
Note : Don't show interstial ad's in first activty of your apps. If you do that apps are going to approve to publish in the play store. before integrating admob in your application read admob policies carefully.
0 comments
Please leave your comments...... Thanks