Android How to integrate Admob

sapan
In this post I will be describe how can one integrate admob into their application.

For integrating admob you can integrate it as standalone or with firebase.

In this post I will be describing how can you integrate it as standalone.

Things required to display the admob.
1. Your app probably  android (using android studio)
2.  Admob account
3. Your app has permission to access internet.



First lets get started with Admob account creation, It is pretty straight forward.
visit : https://www.google.co.in/admob/ and create your account.

1.After creating the account. on dashboard click "monetize new app" button.
2. After that you need to add your app. You can either select your app from play store. Or you can add it manually( for manual enter the package name of your application).


3.After adding your app you need to set which type of ad unit you need to add to your app. Currently
   Banner, Interstitial, reward video and native ads are support. (currently we will go with banner ad.)

4. For now firebase can be skip.
5. After adding you will get your app-Id and ad-unit id.
6.It is not recommended to use your ad unit during testing.You can use the demo ad unit.

Now once you have setup up your ad-unit now let integrate admob to app

1. Add the library to your build.gradle [ add it to your app build file (module:app)]

compile 'com.google.android.gms:play-services-ads:10.2.4'
2. Sync the project.
3. Now let initialize  the admob in the activity.


public class MainActivity extends AppCompatActivity {
    ...
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Sample AdMob app ID: your app ad id
        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");    }
    ...
}

4. Now lets add the banner ad-view to the layout. Open your activity layout and add.

<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"
        android:background="@color/blue">
    </com.google.android.gms.ads.AdView>

5.Now you need to initialize the adview so that ads starts coming in your activity.

public class MainActivity extends AppCompatActivity {
    private AdView mAdView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobileAds.initialize(getApplicationContext(),
            "ca-app-pub-3940256099942544~3347511713");

// init the ads
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
    ...
}

6. Now run the app in your device you will see demo ads showing in the app.


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !