Admob Banner in all activities of android app.

Creating Admob Banner is very easy to implement in android app.
1) create app in Admob Console and Get Admob unit id of banner. 2) use following in your layout xml file

<com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

3) use following to request ad in banner view in your activity.

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

looks good , but what if your app is having so many activities and you want to put banner on all those.


1) create banner in all activity xml layouts and use above steps for all activities. Doesn’t seems impressive. huhhh…


2) Have some solution which bring banner working with less efforts. Let’s try with android.R.id.content
It’s a root view [FrameLayout] of all activities in android app. So we can add banner to this view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner1_ad_unit_id" >
    </com.google.android.gms.ads.AdView>

</LinearLayout>
@SuppressLint("NewApi")
	protected void setupAdAtBottom() {
		content = (FrameLayout) findViewById(android.R.id.content);
		// inflate ad layout and set it to bottom by layouparams
		final LinearLayout ad = (LinearLayout) getLayoutInflater()
				.inflate(R.layout.ad_layout, null);
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.BOTTOM;
		ad.setLayoutParams(params);

		// adding viewtreeobserver to get height of ad layout , so that
		// android.R.id.content will set margin of that height
		ViewTreeObserver vto = ad.getViewTreeObserver();
		vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			@Override
			public void onGlobalLayout() {
				if (Build.VERSION.SDK_INT < 16) {
					ad.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				} else {
					ad.getViewTreeObserver().removeOnGlobalLayoutListener(this);
				}
				int width = ad.getMeasuredWidth();
				int height = ad.getMeasuredHeight();
				Log.i("ad hight", height + "");
				setSpaceForAd(height);

			}

		});
		addLayoutToContent(ad);

	}

	private void setSpaceForAd(int height) {

		// content.getChildAt(0).setPadding(0, 0, 0, 50);
		View child0 = content.getChildAt(0);
		FrameLayout.LayoutParams layoutparams = (android.widget.FrameLayout.LayoutParams) child0
				.getLayoutParams();
		layoutparams.bottomMargin = height;
		child0.setLayoutParams(layoutparams);

	}

	private void addLayoutToContent(View ad) {
		// content.addView(ad);
		content.addView(ad);
		AdView mAdView = (AdView) ad.findViewById(R.id.adView);
		AdRequest adRequest = new AdRequest.Builder().build();
		mAdView.loadAd(adRequest);
	}

Back To All Posts