AdMob Banner Ads in Android Studio
Show an adaptive AdMob banner at the bottom of your app, with test ads while you build and your real IDs kept out of the code.
What you'll learn
- Add the Google Mobile Ads SDK
- Show an adaptive banner that fits every phone
- Use test ads in debug and your IDs in release
- Pause and free the banner with the screen
Before you start
- Android Studio (any recent version)
- Basic Java: variables, methods and if/else
- A free AdMob account
- An emulator or an Android phone
How it fits together
- 1MobileAds.initialize (background)
- 2AdView with adaptive size
- 3loadAd(AdRequest)
- 4Banner at the bottom
- 5pause / resume / destroy
Build it step by step
1. Create the project
In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24.
2. How AdMob IDs work
AdMob uses two IDs: an app ID (~ in the middle) for your whole app, and an ad unit ID (/ in the middle) for each ad spot. Google publishes test IDs that always show safe test ads. Use them while you build.
3. Keep your IDs out of the code
In app/build.gradle.kts, add the import, the localProperties loader, the two build types, buildConfig = true, and the dependency, exactly as shown in Required setup. The dependency line is:
implementation("com.google.android.gms:play-services-ads:25.5.0")Press Sync Now. Debug builds now get Google's test IDs, and release builds read yours from local.properties, which is never committed. Your code reads the ad unit ID as BuildConfig.BANNER_AD_UNIT_ID.
4. Add the app ID to the manifest
Inside <application> in AndroidManifest.xml, add the APPLICATION_ID meta-data with the value ${ADMOB_APP_ID}. Gradle fills it in. Without an app ID, the app crashes on launch. The SDK adds the internet permission for you.
5. Make room for the banner
In activity_main.xml, put your content in a ScrollView with layout_weight="1", and an empty FrameLayout (adContainer) under it. The banner is added there in code.
6. Start the SDK
Call MobileAds.initialize once, on a background thread, so the app opens quickly.
7. Create an adaptive banner
Create an AdView, set the ad unit ID, and give it AdSize.getLargeAnchoredAdaptiveBannerAdSize(this, widthDp). Adaptive banners fill the screen width and AdMob picks the best height for each phone. Add it to the container and call loadAd.
8. Respect the lifecycle
Call banner.pause() in onPause, resume() in onResume, and destroy() in onDestroy, so the ad stops working when it can't be seen.
9. Run it
A banner labelled Test Ad appears at the bottom.
Publish with your own ads
- In the AdMob console, add your app and create an ad unit.
- Add both IDs to
local.properties:
ADMOB_APP_ID=ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx
ADMOB_BANNER_ID=ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx- Build a release version. Keep testing with debug builds: tapping your own live ads can get your AdMob account suspended.
What's improved over the video
The current SDK (25.5.0), adaptive banners that fit every phone, the SDK started off the main thread, the lifecycle handled, and your real IDs kept out of the code.
Files affected
Check what to add and what to replace before copying code.
- app/src/main/AndroidManifest.xmlMerge additions
- app/build.gradle.ktsAdd these lines
- local.properties (project folder)Add these lines
- app/src/main/java/com/example/admobbanner/MainActivity.javaReplace file
- app/src/main/res/layout/activity_main.xmlReplace file
- app/src/main/AndroidManifest.xmlReplace file
- app/src/main/java/com/example/admobbanner/Screens.javaAdd these lines
- app/src/main/res/values/strings.xmlReplace file
- app/src/main/res/values/themes.xmlReplace file
- app/src/main/res/drawable/tip.xmlAdd these lines
Required setup
These are additions to an existing project—not replacement project files.
Add the AdMob app ID
Merge additionsWhere: app/src/main/AndroidManifest.xml
Add this inside <application>. Gradle fills in the value.
<!-- Inside <application> -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="${ADMOB_APP_ID}" />
Add the Ads SDK and keep IDs out of code
Add these linesWhere: app/build.gradle.kts
Add each part where its comment says, then press Sync Now.
import java.util.Properties
// At the very top of app/build.gradle.kts, after the imports
val localProperties = Properties().apply {
rootProject.file("local.properties").takeIf { it.exists() }?.inputStream()?.use { load(it) }
}
// Inside android { ... }
buildTypes {
// Debug builds always show Google's test ads, so you can never click your own real ads by mistake.
debug {
manifestPlaceholders["ADMOB_APP_ID"] = "ca-app-pub-3940256099942544~3347511713"
buildConfigField("String", "BANNER_AD_UNIT_ID", "\"ca-app-pub-3940256099942544/9214589741\"")
}
// Release builds use your own IDs from local.properties.
release {
manifestPlaceholders["ADMOB_APP_ID"] = localProperties.getProperty("ADMOB_APP_ID", "")
buildConfigField("String", "BANNER_AD_UNIT_ID", "\"${localProperties.getProperty("ADMOB_BANNER_ID", "")}\"")
}
}
buildFeatures {
buildConfig = true
}
// Inside dependencies { ... }
implementation("com.google.android.gms:play-services-ads:25.5.0")
Your real IDs (for release)
Add these linesWhere: local.properties (project folder)
Never committed, so your IDs stay out of your code and off GitHub.
# Only needed when you publish. Debug builds use test ads.
ADMOB_APP_ID=ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx
ADMOB_BANNER_ID=ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx
Application code
Select a file to see its code and where it belongs.
This code uses package com.example.admobbanner. Change that first line to your own package, the one at the top of your MainActivity.
app/src/main/java/com/example/admobbanner/MainActivity.javaStarts the SDK, creates an adaptive banner and loads it; pauses and frees it with the screen.
package com.example.admobbanner;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.FrameLayout;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
/**
* A screen with an adaptive AdMob banner pinned to the bottom.
* Adaptive banners pick the best height for each phone's width, so they look right everywhere.
*/
public class MainActivity extends AppCompatActivity {
private AdView banner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Screens.setUp(this, R.layout.activity_main);
// Start the Mobile Ads SDK once, off the main thread, so the app opens quickly.
new Thread(() -> MobileAds.initialize(this, status -> {})).start();
banner = new AdView(this);
// The ad unit ID comes from app/build.gradle.kts: a test ID while you develop, yours in release.
banner.setAdUnitId(BuildConfig.BANNER_AD_UNIT_ID);
banner.setAdSize(adaptiveSize());
FrameLayout container = findViewById(R.id.adContainer);
container.addView(banner);
banner.loadAd(new AdRequest.Builder().build());
}
/** The banner fills the screen width; AdMob chooses the height. */
private AdSize adaptiveSize() {
DisplayMetrics metrics = getResources().getDisplayMetrics();
int widthDp = (int) (metrics.widthPixels / metrics.density);
return AdSize.getLargeAnchoredAdaptiveBannerAdSize(this, widthDp);
}
// Pause the banner when the screen is hidden, and free it when the screen closes.
@Override
protected void onPause() {
banner.pause();
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
banner.resume();
}
@Override
protected void onDestroy() {
banner.destroy();
super.onDestroy();
}
}


