26

I get a "Required XML attribute "adSize" was missing" when I launch my app. This is written on the screen of my smartphone instead of my banner.

I tried the different solutions founded on stack (like xmlns:ads="http://schemas.android.com/apk/res-auto" instead of xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads" or instead of xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads") but it doesn't work.

Here's my java code (a simple Hello World just to try to implement a banner):

package com.example.publicite; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

Here's my xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads" android:id="@+id/adView" android:layout_width="fill_parent" android:layout_height="wrap_content" ads:adSize="BANNER" ads:adUnitId="MyAdUnitId" ads:loadAdOnCreate="true" ads:testDevices="TEST_EMULATOR" /> <Button android:id="@+id/votrescore" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="..." /> </RelativeLayout> 

and here's my manifest:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.publicite" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="13" android:targetSdkVersion="13" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> <activity android:name="com.example.publicite.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> </manifest> 

I use the google play service lib.

2
  • I think you don't need to redefine your ads namespace in your AdView. Try removing this line xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads from <com.google.android.gms.ads.AdView/> Commented Mar 26, 2014 at 21:27
  • i get erros if i do this: no ressource identifier for loadoncreate and testdevices Commented Mar 26, 2014 at 21:40

19 Answers 19

37

Try changing your declaration this way:

<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" ads:adUnitId="YOUR_AD_UNIT_ID" ads:adSize="BANNER"/> 

---- EDIT ----

You can also try to do it programmatically. Simply, instead of an AdView layout item, define a LinearLayout and by code do something like this:

final AdView adView = new AdView(getActivity()); adView.setAdUnitId("YourId"); adView.setAdSize(AdSize.BANNER); final LinearLayout adLinLay = (LinearLayout) view.findViewById(R.id.your_defined_linearlayout); adLinLay.addView(adView); final AdRequest.Builder adReq = new AdRequest.Builder(); final AdRequest adRequest = adReq.build(); adView.loadAd(adRequest); 
Sign up to request clarification or add additional context in comments.

6 Comments

as I said, i've already tried this technic to just remove the 's' from the "libs" but even with xmlns:ads="schemas.android.com/apk/lib/com.google.ads" it doesn't work. I have the same problem :'(
But even removing the s you still see the Required XML attribute "adSize" was missing error?
Yes I was just thinking about it. I've also found this on internet:developers.google.com/mobile-ads-sdk/docs/admob/…
It is written that the namespaces have changed .../res-auto it the right one and the attributes loadOnCreate don't exist anymore...Obviously I have to do an app when there is this migration. What a luck !
It's really helpful. i was try with this -> "xmlns:ads="schemas.android.com/apk/lib/com.google.ads" so i got this error and when i try with this -> "xmlns:ads="schemas.android.com/apk/res-auto" then it is work fine.
|
14

Here is a weird answer. I just restarted my eclipse and it stared working. This might help some one.

Just restart eclipse, android studio or IntelliJ IDE

1 Comment

this is not an answer this is a prove that the error was the person between the keyboard and the chair
9

I have same problem.

Solution for me:

 xmlns:ads="" 

changed to

xmlns:ads="http://schemas.android.com/apk/res-auto" 

Comments

8

Problem fixed for me by changing name space uri

from

 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 

to

xmlns:ads="http://schemas.android.com/apk/res-auto" 

1 Comment

That's actually the 'app' namespace uri. Thus you might as well just delete the xmlns:ads=... namespace alltogether and change the 'ads' prefix to 'app' in the corresponding lines.
6

I had the same problem and I corrected it with the xmlns labels. I put two "xmlns:ads" labels (one for "res-auto" in the AdView and other for "tools" in the parent layout). I had to use two labels for "res auto", one with xmlns:app in the FloatingActionButton and other with xmlns:ads in the AdView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.v7.widget.Toolbar android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="@dimen/fragment_start_margin" android:background="@drawable/fondo_proyectos" android:elevation="4dp" android:gravity="bottom" android:minHeight="@dimen/toolbar_height"> </android.support.v7.widget.Toolbar> <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/av_bottom_banner" ads:adSize="BANNER" ads:adUnitId="@string/banner_inferior_ad_unit_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/av_bottom_banner" android:fitsSystemWindows="true"> <android.support.design.widget.FloatingActionButton xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/av_bottom_banner" android:layout_alignParentEnd="true" android:layout_gravity="bottom|end" android:layout_marginLeft="@dimen/fab_margin" android:layout_marginRight="@dimen/fab_margin" android:elevation="4dp" android:src="@android:drawable/ic_input_add" android:tint="@android:color/white" app:fabSize="normal" app:srcCompat="@drawable/ic_account_circle" /> <include layout="@layout/proyecto_content" /> </android.support.design.widget.CoordinatorLayout> </RelativeLayout> 

Comments

3

I faced the same error. but I couldn't fix it with the pinned answer. I changed the xml to be like this.

<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> 

The main changes are: xmlns:ads="http://schemas.android.com/apk/res-auto" and ads:adSize="BANNER". I recorded my laptop screen while solving this issue. So If you like visual illustrations, you can see the video on YouTube.

Comments

1

change as below ..remove ads:loadAdOnCreate="true"

 <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="fill_parent" android:layout_height="wrap_content" ads:adSize="BANNER" ads:adUnitId="MyAdUnitId"/> 

check this link, it has clear example

https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#play

5 Comments

yes i did it. I remove this and the other attribute testdevice and now i have no more error. But i have no ad, just a button...I think the first atribute is important to add an ad
Yes i just copy it from admob (monetizing section). But it's true, when i put something else like "3231165" for my ad id, i've got the same result: just a button and nothing inside. But I think the problem is because there's no more the attribute loadOnCreate, that is banned now due to their migration: developers.google.com/mobile-ads-sdk/docs/admob/…
hope you have done the code and manifest changes . adView.loadAd(adRequest); check the link I shared
Yes i'm trying to do it with a bit java now. Thanks
Link in answer is dead (Page not found | 404. That's an error.)
1

This happen when eclipse failed to syncronize your xml with google play library, try cleaning project and restart eclipse

Comments

1

Juste clean your app !

Build -> Clean Project

The message dissapear.

Comments

0

It turns out that you CANNOT use wrap_content as a size, so you need to give it a size instead. My ads weren't working and after a day of googling I changed my

android:layout_height="wrap_content"

to

android:layout_height="100dp"

and it fixed the issue. P.S. "match_parent" does work (I have that set for my width)

Comments

0

Check for DEIVCE_ID_EMULATOR too as in my code I have the following code from following the tutorial from Google:

AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build(); 

In my case I was testing it on a real device so it never came up but just gave me the message that the adsize was not set. I ran up an emulator and nkn's solution works perfectly.

Here is my complete code: In main activity:

AdView adView = (AdView)findViewById(R.id.adView); AdRequest adRequest = newAdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build(); adView.loadAd(adRequest); 

and my XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <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" ads:adUnitId="@string/UNIT_ID" ads:adSize="BANNER"/> </LinearLayout> 

NOW run the app on your device and check out your logcat as it will show the line: I/Ads: Use

AdRequest.Builder.addTestDevice("33BE2250B43518CCDA7DE426D04EE232") to get test ads on this device. 

Copy this id (DIFFERENT FOR YOUR DEVICE) to the .addTestDevice as follows:

AdView adView = (AdView)findViewById(R.id.adView); AdRequest adRequest = newAdRequest.Builder() .addTestDevice("33BE2250B43518CCDA7DE426D04EE232") .build(); adView.loadAd(adRequest); 

AND That is how I fixed this issue in my case.

Hope it helps.

Comments

0
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adMobBanner" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"`enter code here` android:layout_centerVertical="true" /> 

Comments

0

Add this in android manifest:

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" /> 

That solved my problem, I hope it will solve yours.

1 Comment

Please remember to always include code as text and not image.
0
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView1" android:layout_width="match_parent" android:layout_height="wrap_content" ads:adUnitId="ca-app-pub-39402560999425544/1033173712" // ID from google admob ads:adSize="BANNER"/> 

Comments

0

Create your XML file as below and do not set AdUnitID and adSize in Java file after setting it in XML.

 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto"> <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_alignParentBottom="true" android:layout_centerHorizontal="true" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" ads:adUnitId="ca-app-pub-3940256099942544/6300978111" ads:adSize="BANNER"/> </androidx.constraintlayout.widget.ConstraintLayout> 

Here's the java code to

MobileAds.initialize(context, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } }); AdView mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build(); mAdView.loadAd(adRequest); 

Comments

0

Late to the party, you are getting this error because you're not using ads namespace in your Banner's XML Code you should use like this.

 <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" ads:adSize="BANNER" ads:adUnitId="@string/ad_banner_id" /> 

Comments

0

Your minimum SDK version is 13 change it 16 by Google documentation And maximum SDK version also 13 change it 28 It should be work perfectly

Comments

0

Best solution for this. Remember to add this line to root layout:

 xmlns:ads="http://schemas.android.com/apk/res-auto" 

full code:

<com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:ads="http://schemas.android.com/apk/res-auto" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/> 

Comments

0

It is a namespace issue, change the namespace URI

from

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 

to

xmlns:ads="http://schemas.android.com/apk/res-auto" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.