Sunday, 1 March 2015

How to Post Admob's banner ad at the bottom of a custom view/ surface view (Programmeticaly).

In this post we will see how you can put Admob's banner ad at the very bottom of your custom view or SurfaceView Programmeticaly with the latest way of integrating Admob.

I hope you already know how to add a normal banner ad. If not please hit this link to know super easy way.


------------------------------------ build.gradle ------------------------------------
apply plugin: 'com.android.application'

android {
compileSdkVersion
29

defaultConfig {
applicationId
"com.app.yourappname"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled
false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir:
"libs", include: ["*.jar"])
implementation
'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.gms:play-services-ads:19.2.0'
}
 ----------------------------------------------------------------

Follow these steps for easy understanding:
1) Initialize the Admob ads.
2) Initialize your custom View by extending View to it
3) Create a RelativeLayout
4) Add your custom View to RelativeLayout
5) Add the admob banner ad to RelativeLayout
6) Set content view to RelativLayout
------------------------------------------ CustomViewActivity.java -------------------------------------
package com.app.yourappname;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

import androidx.annotation.Nullable;

public class CustomViewActivity extends Activity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});

MySimpleView myView = new MySimpleView(this);

AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); //AD_UNIT_ID is your banner ad id.

AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams rl_lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

rl_lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(myView);
rl.addView(adView, rl_lp);
setContentView(rl);
}

public class MySimpleView extends View {

private Paint paint = new Paint();

public MySimpleView (Context context) {
super(context);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.rgb(0,255,0));
canvas.drawRect(new Rect(0,0,canvas.getWidth(), canvas.getHeight()), paint);
}
}
}
--------------------------------------------------------------------------

----Add the internet permissions, app id and declare Activity name with landscape orientation  ------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.app.yourappname">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"
/>
<activity android:name=".CustomViewActivity" android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
--------------------------------------------------------------------------