Mehedi Hassan
Mehedi Hassan

Reputation: 87

AdMob integration in Android

I am implementing AdMob in my app. I have added xml codes.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="350dp"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-2415896980984028/6183643605" >
    </com.google.android.gms.ads.AdView>

</LinearLayout>

I have added this codes in my activity_main.xml ..Also i have added this code in my MAinActivity class. But when i installed the app in my device for testing it crashed.

    adView=(AdView) this.findViewById(R.id.adView);
    adView.setVisibility(AdView.VISIBLE);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

I cannot figure out what is the problem actually.

Upvotes: 2

Views: 159

Answers (3)

Parag Chauhan
Parag Chauhan

Reputation: 35976

You have to write code in Activity

  private AdView adView;
    private static final String AD_UNIT_ID = "INSERT_YOUR_AD_UNIT_ID_HERE";
    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);

Must ad this line in Menifest.

  <activity android:name="com.google.android.gms.ads.AdActivity"
   android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
   <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Upvotes: 1

Nana Ghartey
Nana Ghartey

Reputation: 7927

Since you're using the admob via the google play service library, add/make the following changes:

In your layout, Change the ad xml namespace from:

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

   -----/>

To :

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

   ----/>

Then in your manifest's application tag, Change:

<activity android:name="com.google.ads.AdActivity"/>

to:

<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

Add:

<meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>

Don't forget the permissions:

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

Upvotes: 1

Android
Android

Reputation: 9033

have you added this in manifest.xml?

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<activity android:name="com.google.ads.AdActivity"
 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

and have you added lib?

Upvotes: 1

Related Questions