How to add Admob banner ads in android studio? Banner ads are a rectangular image or text ad that occupies one place in the layout of the app. If you are new to mobile advertising, banner ads are the easiest to implement. This article shows you how to integrate AdMob’s banner ads into the Android app. Banners There are two ways we can implement banners and smart banners.
Contents
Banner Ads
First, create a new project in Android Studio and add the following codes to import the Google Mobile Ads SDK.
In the project-level build.Gradle file, add the highlighted code to the all projects section.
allprojects { repositories { google() jcenter() } }
In the app-level build.Gradle file, add the highlighted code to the dependencies section.
dependencies { implementation fileTree(dir : 'libs', include : [ '*.jar' ]) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.google.android.gms:play-services-ads:19.3.0' }
Add your Admob App Id in the AndroidManifest.xml file between the “<application> </application>” tag like below.
<
application
>
<
meta-data
android:name
=
"com.google.android.gms.ads.APPLICATION_ID"
android:value
=
"@string/admob_app_id"
/>
</
application
>
Add the following code to MainActivity.java to initialize Mobile Ads SDK (this only needs to be done once in the app lifecycle). You can find the App ID in the AdMob console.
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.initialization.InitializationStatus; import com.google.android.gms.ads.initialization.OnInitializationCompleteListener; public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the Mobile Ads SDK MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { Toast.makeText(this, " sucesfull ", Toast.LENGTH_SHORT).show(); } });
AdView mAdView;
mAdView = findViewById(R.id.adView);AdRequest adRequest =
new
AdRequest.Builder().build();
mAdView.loadAd(adRequest); } }
Add the highlighted code to activity_main.xml to show the banner ad.
activity_main.xml file:
<?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_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- set Banner ad position in UI layout design --> <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="@string/admob_banner_id"> </com.google.android.gms.ads.AdView> </RelativeLayout>
Add the Admob App Id and Banner Ad Id to string.xml.
strings.xml file:
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
resources
>
<
string
name
=
"admob_app_id"
>
ca-app-pub-3940256099942544~3347511713</
string
>
<
string
name
=
"admob_banner_id"
>
ca-app-pub-3940256099942544/6300978111</
string
>
</
resources
>
Smart Banner Ads
Banners and smart banner ads are implemented on the same dependency, do not have to add some different dependencies to it. All we have to do is change the activity and the XML file, which is something like this.
Add the following code to MainActivity.java to initialize Mobile Ads SDK (this only needs to be done once in the app lifecycle). You can find the App ID in the AdMob console.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
public class MainActivity extends AppCompatActivity {
AdView mAdView;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the Mobile Ads SDK
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
Toast.makeText(this, " sucesfull ", Toast.LENGTH_SHORT).show();
}
});
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
Add the highlighted code to activity_main.xml to show the banner ad.
activity_main.xml file:
<?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_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- set Banner ad position in UI layout design --> <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="SMART_BANNER" ads:adUnitId="@string/admob_banner_id"> </com.google.android.gms.ads.AdView> </RelativeLayout>
In the XML file, we have to change banner ads to make smart banner ads, after this, what we see on the screen will be Smart banner ads. Which will show according to the width of your screen.