VD63
VD63

Reputation: 133

Declaring an AdView in XML

I'm struggling to add some ads in my application and I'm systematically facing the error message that the adSize parameter is missing. My XML file is the following :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/Window"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".myApp">

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adUnitId="4sdfgsd5gs4df5g5sd"
    ads:adSize="BANNER"
    ads:testDevices="TEST_EMULATOR,4sdfgsd5gs4df5g5sd"
    ads:loadAdOnCreate="true"/>

<FrameLayout
    android:id="@+id/Cont"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="top" >

    <LinearLayout
        android:id="@+id/Controls"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal" >

        <mypackage.utils.myapp.view1
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">            
        </mypackage.utils.myapp.view1>

        <ImageButton
            android:id="@+id/Lum"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/my_drawable1"
            android:text="@string/Lum">            
        </ImageButton>

        <ImageButton
            android:id="@+id/Photo"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/my_drawable2"
            android:text="@string/Photo">           
        </ImageButton>

    </LinearLayout>
</FrameLayout>

I have read answers to similar questions posted on this site but, so far, nothing prevented the error message from being triggered. All these actions didn't change anything :

I can add that I don't have any "Attrs.xml" files in my project but, as an answer reminds that it's not mandatory anymore with the new version of AdMob, this shouldn't be a problem.

What is wrong with my XML file?

Upvotes: 0

Views: 1065

Answers (3)

dogger20011
dogger20011

Reputation: 255

I'm not sure what you have in your activity, but as far as the XML goes, you should use:

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

Instead of:

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

And your AdView should look like this:

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

If this doesn't work, then add your activity so we can make sure that you have it all entered correctly as well. Also, make sure you are using the Google Play Services API.

EDIT Add this to your Manifest

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Upvotes: 1

fatih inan
fatih inan

Reputation: 154

Create global variables

/** The view to show the ad. */
private AdView adView;
/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "YOUR_UNIT_ID";

in oncreate method of the activity

// Look up the AdView as a resource and load a request.
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

in xml file

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

the permissions in the androidManifest.xml file

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Upvotes: 0

Naren
Naren

Reputation: 1

Adding a add is pretty simple just follow these steps:

Just paste this code in the activity.

// Create an ad.

private static final String AD_UNIT_ID = "****************************";

adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);

// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);

// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
    .build();

// Start loading the ad in the background.
adView.loadAd(adRequest);

The main thing to check is: LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);

make sure you give the id of the layout of xml you are using for you activity.

Upvotes: 0

Related Questions