-
Create a banner with AdMob in an Android application is a process that involves several steps. Below, I will provide you with a basic step-by-step tutorial using Java or Kotlin and the AdMob SDK.
Step 1: AdMob Configuration
- Create an AdMob account: If you don’t have an AdMob account yet, go to AdMob and sign up.
- Create an app and an ad:
- After logging in to AdMob, create a new app and a new banner ad.
- Get the ad unit ID:
- Copy the ad unit ID you have created, as you will need it in your app’s code.
Step 2: Android Project Setup
- Open your project in Android Studio:
- Make sure you have Android Studio installed and open your project.
- Add the AdMob dependency:
- Open your
build.gradle
file (Module: app) and make sure you have the following dependency:
- Open your
implementation 'com.google.android.gms:play-services-ads:22.5.0'
Also, add the application ID (I recommend saving it in your app’s strings):
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="@string/ads_app" />
Change the content of @string/ads_app to your application ID.
Sync the project:
- Sync your project so that dependencies are downloaded and installed.
Step 3: Implementing the Banner in Your Activity
- Create an exportable banner:
- Create a new file named banner.xml inside res > layout and now add this content:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_alignParentBottom="true" android:orientation="vertical"> <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_gravity="center" ads:adSize="BANNER" ads:adUnitId="your_ad_unit_id "/> </LinearLayout>
In your_ad_unit_id you’ll need to add your AdMob ad unit id.
*I recommend you save the ad unit id in Strings so you can quickly change it in the future.
For that:
- Go to res > values > strings
- Add a new key, with your ad unit id.
<string name="ads_banner" translatable="false">your_ad_unit_id</string>
- To use it you’ll have to put:
ads:adUnitId="@stringads_banner"
2. Add the banner to the View where you want to display it:
<include layout="@layout/banner"></include>
And that’s it:
Java code:
Open your Java file (
MainActivity.java
for example) and add the following code to load the banner:import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class MainActivity extends AppCompatActivity { private AdView adView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Find the AdView by its ID adView = findViewById(R.id.adView); // Create an ad request AdRequest adRequest = new AdRequest.Builder().build(); // Load the ad into the AdView adView.loadAd(adRequest); } @Override protected void onDestroy() { // Release resources when the activity is destroyed if (adView != null) { adView.destroy(); } super.onDestroy(); } }
Kotlin code:
import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.AdView class MainActivity : AppCompatActivity() { private lateinit var adView: AdView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Find the AdView by its ID adView = findViewById(R.id.adView) // Create an ad request val adRequest = AdRequest.Builder().build() // Load the ad into the AdView adView.loadAd(adRequest) } override fun onDestroy() { // Release resources when the activity is destroyed adView.destroy() super.onDestroy() } }
Step 4: Permissions in the Manifest
Make sure you have internet access permission in your
AndroidManifest.xml
file:<uses-permission android:name="android.permission.INTERNET" />
Step 5: Run the Application
That’s it! Now you should be able to run your application and see the AdMob banner at the top or bottom of your activity.
Please note that it may take some time for ads to appear after implementation due to AdMob’s settings and ad loading.
Extra: Test Code
I share the default test code with which it is recommended to run any ad you want to display in the development environment to avoid being penalized by Google AdMob:
<string name="ads_app" translatable="false">ca-app-pub-3940256099942544~3347511713</string> <string name="ads_banner" translatable="false">ca-app-pub-3940256099942544/6300978111</string>
Create a banner with AdMob on Android using Java or Kotlin
Tiempo de lectura: 3 minutos