AdMob in a fragment

11,194

Change your layout in this way:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <com.google.android.ads.AdView
        android:id="@+id/fragment_03_admob"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:adSize="BANNER"
        app:adUnitId="@string/activity_admobID"
        app:loadAdOnCreate="true"
        app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" />

</LinearLayout>

You was using a wrong namespace declaration.

Share:
11,194
Lucian
Author by

Lucian

Updated on September 01, 2022

Comments

  • Lucian
    Lucian almost 2 years

    I'm inserting an AdMob in my application. but I have a problem about it.

    when I inserted some codes about it in Eclipse, I can see an error message about "the constructor AdRequest() is not visible " in a fragment_03 and "error: Error parsing XML : unbound prefix" from "

    so I attached source for the AdMob as follows.

    Can you give me an advice ?

    Let me know if you have any question.

    Thanks

    • fragment_03.java

      import com.google.ads.AdRequest;
      import com.google.ads.AdView;
      
      public class Fragment_03 extends Fragment {
      
      private AdView adView;
      private AdRequest adRequest;
      
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_03, container, false);
      
          adRequest = new AdRequest();
          adView = (AdView) rootView.findViewById(R.id.fragment_03_admob);
          adView.loadAd(adRequest);
      
          return rootView;
          }
      }
      
    • fragment_03.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
          <com.google.android.ads.AdView
              xmlns:ads="schemas.android.com/apk/lib/com.google.ads"
              android:id="@+id/fragment_03_admob"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:adSize="BANNER"
              app:adUnitId="@string/activity_admobID"
              app:loadAdOnCreate="true"
              app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" />
      
      </LinearLayout>
      
    • AndroidManifest.xml

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