Search code examples
androidandroid-layoutadmobads

Problems adding new AdMob to android app


I am trying to add the new AdMob ads to my first application, I managed to arrange the code in the correct way but I have to problems:

First problem is my ad ID ca-app-pub-61413779example is getting multiple errors like: ca cannot be resolved to a variable, ba cannot be resolved to a variable, the literal 61413779 of type int is out of range.

The second problem is R.id.mainLayout, which is mainLayout, I don't get it.

 protected void onCreate(Bundle savedInstanceState) {        
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_basic_screen);


     // Create the adView
        adView = new AdView(this, AdSize.BANNER, ca-app-pub-61413779example);

        // Lookup your LinearLayout assuming it's been given
        // the attribute android:id="@+id/mainLayout"
        LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);


        // Add the adView to it
        layout.addView(adView);
        adView.loadAd(new AdRequest());

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());

Solution

  • I'm not familiar with your admob adding way, but I got another great way to add admob to your app using xml only.

    first in your manifest add these permissions

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

    * the second is most important

    then add this in your manifest inside the application tag

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

    now in every activity you want the ad to appear on you add this in the layout.xml of the activity

    <com.google.ads.AdView
        android:id="@+id/ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adUnitId="xxxxxxxxxxxx"
        ads:adSize="BANNER"
        ads:loadAdOnCreate="true" 
        android:layout_gravity="center"
        android:layout_margin="10dp">
    </com.google.ads.AdView>
    

    and inside the top parent layout ex. linearlayout you add these under xmlns:android="xxx

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

    now the ads should work perfectly :D

    UPDATE: don't forget to download and place the GoogleAdMobAdsSdk-6.4.1.jar under the libs folder.