Android google admob implementation – Banner

Android google admob implementation – Banner

Before Follow this tutorial please first complete the project configuration about how to integrate admob in android . 

 

Step 1- Set banner ads on layout :

Set banner elements on layout file

  
<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">
</com.google.android.gms.ads.AdView>

As like –

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools"
 android:layout_height="match_parent" 
android:layout_width="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin"
 tools:context=".MainActivity">
<TextView android:text="@string/hello_world" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" />

<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">
</com.google.android.gms.ads.AdView>

</RelativeLayout>

Note the following required attributes:

  • ads:adSize – Set this to the ad size you’d like to use. If you don’t want to use the standard size defined by the constant, you can set a custom size instead. See the banner size section below for details.
  • ads:adUnitId – Set this to the unique identifier given to the ad unit in your app where ads are to be displayed. If you show banner ads in different activities, each would require an ad unit.

You can alternatively create AdView programmatically:

AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
// TODO: Add adView to your view hierarchy.

Step 2- Load Banner ads :

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);


As like - 

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {
    private AdView mAdView;

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

        MobileAds.initialize(this,
            "ca-app-pub-3940256099942544~3347511713");

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}


Step 2- Banner ads events :

 
mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        // Code to be executed when an ad finishes loading.
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        // Code to be executed when an ad request fails.
    }

    @Override
    public void onAdOpened() {
        // Code to be executed when an ad opens an overlay that
        // covers the screen.
    }

    @Override
    public void onAdLeftApplication() {
        // Code to be executed when the user has left the app.
    }

    @Override
    public void onAdClosed() {
        // Code to be executed when when the user is about to return
        // to the app after tapping on an ad.
    }
});

Leave a Reply

Your email address will not be published. Required fields are marked *