MrJazsohanisharma

Inline adaptive banners, Fixed, Dynamic with Interstitial ads For Android Apps

 Inline adaptive banners For Android Apps 

 Activity Main xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
tools:context=".MainActivity"
android:background="@color/white">

<Button
android:layout_gravity="center"
android:id="@+id/buttonShowAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Add!"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>

<!--Bannar fixed ader Jonno

<com.google.android.gms.ads.AdView
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/banner_ad_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/Admob_App_FixedBanner" />
-->

<!--//Dynamic Banner Add Bannar fixed ar jonno Dynamic korte hobe Jonno -->
<LinearLayout
android:id="@+id/addLayoutDynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">

</LinearLayout>




</RelativeLayout> 


Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>


<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyAdmobInstigation"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/Admob_App_ID"/>
</application>

</manifest> 

ActivityMain.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
tools:context=".MainActivity"
android:background="@color/white">

<Button
android:layout_gravity="center"
android:id="@+id/buttonShowAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Add!"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>

<!--Bannar fixed ader Jonno

<com.google.android.gms.ads.AdView
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/banner_ad_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/Admob_App_FixedBanner" />
-->

<!--//Dynamic Banner Add Bannar fixed ar jonno Dynamic korte hobe Jonno -->
<LinearLayout
android:id="@+id/addLayoutDynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">

</LinearLayout>




</RelativeLayout>

Stirng.xml

<resources>
<string name="app_name">My Admob Instigation</string>
<string name="Admob_App_ID">ca-app-pub-3940256099942544~3347511713</string>
<string name="Admob_App_Interstitials">ca-app-pub-3940256099942544/1033173712</string>
<string name="Admob_App_FixedBanner">ca-app-pub-3940256099942544/6300978111</string>
<string name="Admob_App_Dynamic_Banner_Add">ca-app-pub-3940256099942544/9214589741</string>


</resources>

All Java Code 

package com.shamim.myadmobinstigation;

import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowMetrics;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.gms.ads.AdError;
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.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

public class MainActivity extends AppCompatActivity {
Button buttonShowAdd;
private InterstitialAd mInterstitialAd;
private static final String TAG = "MainActivity";

//Bannar fixed ader Jonno

//BannarfixedaderJonno //AdView banner_ad_view;


//Dynamic Banner Add
LinearLayout addLayoutDynamic;
//Dynamic Banner Add
AdView adView;

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

buttonShowAdd=findViewById(R.id.buttonShowAdd);
//BannarfixedaderJonno //banner_ad_view=findViewById(R.id.banner_ad_view);

//Dynamic Banner Add
addLayoutDynamic=findViewById(R.id.addLayoutDynamic);

new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
})
.start();





//Dynamic Banner Add
dynamicBannerAdd();
LoadFullScreenAdd();
InlineadaptivebannersScrolView();


//Bannar fixed ader Jonno
// AdRequest adRequest = new AdRequest.Builder().build();
//BannarfixedaderJonno banner_ad_view.loadAd(adRequest);




buttonShowAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadInstitialAdd();

}
});









}

//LoadInstitialAdd Method
public void loadInstitialAdd(){
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
}

//LoadFullScreenAdd Method
public void LoadFullScreenAdd(){


AdRequest adRequest = new AdRequest.Builder().build();


InterstitialAd.load(this,getString(R.string.Admob_App_Interstitials), adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
// Set the FullScreenContentCallback
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
@Override
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}

@Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
mInterstitialAd = null;
LoadFullScreenAdd();
}

@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
mInterstitialAd = null;
}

@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}

@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
});



}

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.d(TAG, loadAdError.toString());
mInterstitialAd = null;
}
});


}


//Dynamic Banner Add nicher ta othoba
// public AdSize getSize(){
// DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
// int adWidthPixel=displayMetrics.widthPixels;

// if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.R){
// WindowMetrics windowMetrics=this.getWindowManager().getCurrentWindowMetrics();
// }

// float density = displayMetrics.density;
// int adWidth = (int) (adWidthPixel / density);
// return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this,adWidth);
// }// getSize use na kore nicher taw use korte parbo DynamicBanneradd ar modde AdSize adSize = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(this, 320);




//Dynamic Banner Add
public void dynamicBannerAdd(){
adView = new AdView(this);
adView.setAdUnitId(getString(R.string.Admob_App_Dynamic_Banner_Add));


AdSize adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 360);
adView.setAdSize(adSize);
// Replace ad container with new ad view.
addLayoutDynamic.removeAllViews();
addLayoutDynamic.addView(adView);

AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

}



//Inline adaptive banners ja Scroolaview ar modde amra vebohar korte parbo
public void InlineadaptivebannersScrolView(){
adView = new AdView(this);
adView.setAdUnitId(getString(R.string.Admob_App_Dynamic_Banner_Add));


AdSize adSize = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(this, 320);
adView.setAdSize(adSize);
// Replace ad container with new ad view.
addLayoutDynamic.removeAllViews();
addLayoutDynamic.addView(adView);

AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

}

}

SHAMIM HOSEN

Girl in a jacket

Post a Comment

Previous Post Next Post