• 8
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Google Admob Android: Working on only one device

I have set up an admob adview in my android app: The manifest:

  <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-123456567787889990">

My xml:

   <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-0987784576567456234511"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintEnd_toEndOf="parent"
        ads:layout_constraintStart_toStartOf="parent">
    </com.google.android.gms.ads.AdView>

In my java code I have done the following:

 MobileAds.initialize(this, "ca-app-pub-123456567787889990");
        AdRequest adRequest = null;
        if (BuildConfig.DEBUG) {
            adRequest = new AdRequest.Builder().addTestDevice("EDDADA7CC97DD3A4AAD9123312312321").build();
        } else {
            adRequest = new AdRequest.Builder().build();
        }
        adView.loadAd(adRequest);

I understand that real ads are only to be used in production and I have to use test ads. However the test ads appear only on the one device(the device that I am testing on), its working properly there. However it does not appear on any other device. I sent an apk to my client and the ad-space always appears blank with no test-ad.

Is this the intented behavior or am I missing something?? Please help.

      • 2
    • Did you try using the demo credentials by the Ad Mob team? It will help you to see a demo Ad in your app and you don't need to add a test device for that too
      • 1
    • @PembaTamang so if the test Ads are working properly so it means your coding part is fine and working, just change the Ad Mob Id(live one) and you are ready to upload in Store. Keep this in mind that ads are location specific, if there are no Ads available in your area it will display nothing.

You have added only one test device for adRequest() in debug mode. So it's working for that device only. First of all, you don't need to test with a live account with real AdMob unit id, you can use test ad unit id. And don't need to check if it is in debug mode or not.

You can follow this way to integrate AdMob test banner ads.

  1. Add this in project-level build.gradle
    allprojects {
        repositories {
            google()
            jcenter()
        }
    }
  1. Add this app-level build.gradle
    dependencies {

        implementation 'com.google.android.gms:play-services-ads:17.1.1'
    }
  1. Then add this in Manifest.xml file inside application tag

    <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="[ADMOB_APP_ID]"/>
    

4.Then In you layout xml file

    <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"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
  1. Then in your activity do this
package ...

import ...
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {
    private AdView mAdView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this,
            "ca-app-pub-3940256099942544~3347511713");

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}

Then it will work perfectly for all devices. After that you have to just replace your admob app id and banner unit id. Hope it will help you.

  • 1
Reply Report