I want to update one source code from old GoogleAds SDK
to new Google Play Service Library
, but there is a problem. Everytime I got this error:
There was a problem getting an ad response.
ErrorCode: 0
Failed to load ad:0
This is code from playactivty:
public String BANNER_AD_UNIT_ID;
AdRequest adRequest;
private void LoadAds()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.adView);
this.BANNER_AD_UNIT_ID = getResources().getString(R.string.admob_id);
// Create the adView
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(BANNER_AD_UNIT_ID);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
// AdRequest.setTesting(true);
adView.loadAd(adRequest);
}
And from layout.xml
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/admob_id">
</com.google.android.gms.ads.AdView>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
What I was doing wrong there? Can not find issue, and trust me, I was read all SOF posts about that error :)
Thank you very much!
Edit:
Original code:
PlayActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
LoadConfigParams();
LoadSharedPreferences();
LoadResources();
LoadListeners();
LoadStage(mCurStage);
LoadAds();
}
private void LoadAds()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayoutAdmob);
// Create the adView
AdView adView = new AdView(this, AdSize.SMART_BANNER, getResources().getString(R.string.admob_id));
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
activity_play.xml
(only last LinearLayout code, above that is RelativeLayout).
<LinearLayout
android:id="@+id/linearLayoutAdmob" android:layout_width="fill_parent"
android:layout_height="wrap_content"></LinearLayout>
There are two ways of creating an Admob
add by java
or xml
, the easiest way-(even though both are easy) is xml
.Also if you use SMART_BANNER
you are going to get adds of different sizes, and if the size does not fit/match the remaining screen space yourRelativeLayout
has left for LinearLayout
your add will not show. so i will assume you have taken care of that
<LinearLayout
android:id="@+id/linearLayoutAdmob"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.google.android.gms.ads.AdView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
android:id="@+id/myaddview"
ads:adUnitId="@string/admob_id">
</LinearLayout>
then in your loadAds()
AdView adView = findViewById(R.id.myaddview); //add the cast
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);
thats all you need check this to know what to import and double check your permissions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With