I tried to implement AdMob into my application, and everything worked fine until i tried to run the application into a device (Xiaomi Redmi note 8 pro).
Here is my xml code:
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<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"
android:layout_marginBottom="5dp"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy">
</com.google.android.gms.ads.AdView>
I replaced ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy with y and x because of private reasons, but that is a Banner ID which i created in AdMob.
The layout shows the preview correct: picture
but when app is being ran nothing appears, not even the empty AdView.
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SchoolTimeTable"
tools:targetApi="31">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
<activity
android:name=".TaskLoadActivity"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".OnSubjectClickActivity"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".AddSubjectActivity"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<meta-data
android:name="android.app.lib_name"
android:value="" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Now the hided (private reasons) number is my app ID, also from AdMob.
And finally here is my OnCreate() in main activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
AdView mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
I am suspicious about xml or manifest.
gradle: implementation 'com.google.android.gms:play-services-ads:22.5.0'
I used exactly your same code, using my own ad ids and it generated the adview for me.
I have a few reasons why it probably won't work for you:
The adview is rendered in the xml view, but that does not represent anything, especially if you do not comment the complete xml of your view. From what I see in the image, when I notice the recyclerview, it is out of place, at least I don't see it aligned with the buttons you have, so it is probably not well connected to the other views, which leads me to your adview Maybe it is loaded but it is not well located, perhaps overlapped with another view.
If your account is new or you have just added the application to your adview account, the ads take time to generate, so they will not be available immediately.
If you are using your personal device, in the case of Xiaomi, you may have an app or DNS that blocks ads.
For either case, you can add a background to the adview layout view, such as a color, to see where it is located and if the ad is generated.
I used Kotlin, but I didn't put anything outside of Java, it's a new project, I added my ad ids, used the same version of the adview dependency and generated the same code in the activity using findViewById.
I hope some of my advice helps you.