How to add Banner Ads in android.

If you not create your own app and banner ad unit in the AdMob UI and use your newly created app ID and ad unit ID in your code. Banner ads are rectangular image or text ads. About integrating Google AdMob in apps and start earning. Remember to configure your device with test ads. More Learn about ad targeting and banner ad guidance.

Create New Project

1. First create a new project in Android Studio (File => New Project)

build.gradle(Module: app)

									   
    compile 'com.google.android.gms:play-services-ads:9.4.0'

AndroidManifest.xml


<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	
	<!--   Add this in below code inside application  part-->
	
	<activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
	
	

string.xml

									   
  <resources>
    <string name="app_name">BannerAdsShow</string>
 <!-- 
For Demo use below Id (  Don't use Original Id to Test ads in you app . For Demo use Replace last three number of you adUnit Id  to  111 for Test ads in your device  )
 -->
   
    <string name="banner_demo">ca-app-pub-1808690293181798/2213111111</string>

</resources>

activity_main.xml

									   						   
   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <com.google.android.gms.ads.AdView
        android:id="@+id/ad_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_demo" />

</RelativeLayout>

Mainactivity.java

									   
  package com.narendra.banneradsshow;

import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

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

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends AppCompatActivity {

    AdView adView;

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

        adView = (AdView) findViewById(R.id.ad_view);

        Log.w("-----------"," Device ID : "+getId(this));

        AdRequest adRequest = new AdRequest.Builder().addTestDevice(getId(this)).build();
        adView.loadAd(adRequest);
    }

    @Override
    public void onPause() {
        if (adView != null) {
            adView.pause();
        }
        super.onPause();
    }

    /**
     * Called when returning to the activity
     */
    @Override
    public void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }

    public static String getId(Context context) {
        String deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        try {
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            digest.update(deviceId.getBytes());
            byte messageDigest[] = digest.digest();

            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {

                String h = Integer.toHexString(0xFF & messageDigest[i]);

                while (h.length() < 2)
                    h = "0" + h;

                hexString.append(h);
            }
            deviceId = hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            deviceId = "";
        } finally {
            return deviceId.toUpperCase();
        }
    }
}

Output :

Tags

  • Android

  • Android Studio

  • Android development

  • Java