Android | AdMob Banner Ads for Android Studio
Banner ads are rectangular image or text ads that occupy a spot within an app’s layout. If you’re new to mobile advertising, banner ads are the easiest to implement.
This article shows you how to integrate banner ads from AdMob into an Android app.
Example –

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 allprojects section.
In the app-level build.gradle file, add the highlighted code to dependencies section.
dependencies { implementation fileTree(dir : 'libs', include : [ '*.jar' ]) implementation 'com.android.support:appcompat-v7:26.1.0' compile 'com.google.android.gms:play-services-ads:15.0.0'} |
Add the following code to Main Activity to initialize Mobile Ads SDK (this only needs to be done once in app lifecycle). You can find the the App ID in AdMob console.
package org.geeksforgeeks.geeksforgeeks; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.google.android.gms.ads.MobileAds; 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, getString(R.string.admob_app_id)); } } |
Add the highlighted code to activity_main.xml to show banner ad.
activity_main.xml –
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android= android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <!-- set Banner ad position in UI layout design --> <com.google.android.gms.ads.AdView xmlns:ads= android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" ads:adSize="BANNER" ads:adUnitId="@string/admob_banner_id" /> </RelativeLayout> |
Add the highlighted code to Main Activity to show Banner Ad.
MainActivity.class –
package org.geeksforgeeks.geeksforgeeks; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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); // Initialize the Mobile Ads SDK MobileAds.initialize(this, getString(R.string.admob_app_id)); // Find Banner ad mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); // Display Banner ad mAdView.loadAd(adRequest); } } |
Add the Admob App Id and Banner Ad Id to string.xml.
strings.xml –
<?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> <!-- ca-app-pub-3940256099942544~3347511713 this is your admob app id --> <!-- ca-app-pub-3940256099942544/6300978111 this is your admob banner ad id --></resources> |
Recommended Posts:
- Android | AdMob Interstitial Ads for Android Studio
- Toasts for Android Studio
- Guide to Install and Set up Android Studio
- Android | Android Application File Structure
- Android | How to add Radio Buttons in an Android Application?
- Android | How to Request permissions in Android Application?
- Android | Starting with first app/android project
- Android | Running your first Android app
- Android | RecyclerView
- Android | 8.0 Oreo
- How to use WebView in Android
- How to use CheckBox in Android
- Android | App to Add Two Numbers
- Adding Firebase to Android App
- Volley Library in Android
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



