To integrate Google AdMob in a mobile application for Android, follow these
steps:
1. Set up your app in your AdMob account.
o Sign in to your AdMob account.
o Click Apps in the sidebar.
o Click Add app.
o Select the platform of your app (Android or iOS).
o Enter a name for your app.
o Confirm that you want to enable user metrics for this app.
o Click Add.
2. Add your AdMob App ID to your AndroidManifest.xml file.
o Open your AndroidManifest.xml file.
o Add the following meta-data tag:
XML
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="YOUR_ADMOB_APP_ID" />
Use code with caution. Learn more
content_copy
o Replace YOUR_ADMOB_APP_ID with your actual AdMob app ID, which you can find in
your AdMob account.
3. Add and initialize the Mobile Ads SDK.
o Add the following dependency to your build.gradle file:
Groovy
implementation 'com.google.android.gms:play-services-ads:20.6.0'
Use code with caution. Learn more
content_copy
o Initialize the Mobile Ads SDK in your app's onCreate() method:
Java
MobileAds.initialize(this);
Use code with caution. Learn more
content_copy
4. Choose an ad format to implement in your app.
o AdMob offers a variety of ad formats, including banner, interstitial, native, and
rewarded ads.
o Choose an ad format that is appropriate for your app and your target audience.
5. Implement the ad format in your app.
o For each ad format that you want to implement, follow the instructions in the
AdMob documentation.
6. Test your ads.
o Once you have implemented ads in your app, test them to make sure that they are
working properly.
o You can do this by using the AdMob test ad unit IDs.
7. Publish your app.
o Once you have tested your ads and are satisfied with them, you can publish your
app to the Google Play Store.
Here is an example of how to implement a banner ad in an Android app:
Java
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
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);
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this);
// Create an AdView.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("YOUR_AD_UNIT_ID");
// Add the AdView to the layout.
LinearLayout layout = findViewById(R.id.layout);
layout.addView(adView);
// Load an ad.
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
Use code with caution. Learn more
content_copy
Once you have implemented ads in your app, you can start earning money from
your app's users