Skip to content

Instantly share code, notes, and snippets.

@GeekTree0101
Created May 23, 2025 05:31
Show Gist options
  • Save GeekTree0101/f29dcbab9bfb953523ef1fde48c78384 to your computer and use it in GitHub Desktop.
Save GeekTree0101/f29dcbab9bfb953523ef1fde48c78384 to your computer and use it in GitHub Desktop.
Admob Bottom Text Ad for Flutter android
<!--
drawable/ad_badge.xml
-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#1A000000" />
<corners android:radius="10dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/native_ad_view"
android:layout_width="match_parent"
android:layout_height="35dp">
<Button
android:id="@+id/ad_call_to_action"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"
android:background="#00FFFFFF"
tools:text="Click" />
<LinearLayout
android:id="@+id/content_container"
android:layout_width="wrap_content"
android:layout_height="54dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/adButton"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:background="@drawable/ad_badge"
android:text="AD"
android:textColor="#333333"
android:textSize="11sp"
android:gravity="center" />
<TextView
android:id="@+id/ad_headline"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:gravity="center_vertical"
android:layout_marginStart="8dp"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxLines="1"
android:text="Ad Title"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
package com.example.app
import android.content.Context
import android.view.LayoutInflater
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView
import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin
class BottomTextAdFactory(private val context: Context) : GoogleMobileAdsPlugin.NativeAdFactory {
override fun createNativeAd(
nativeAd: NativeAd,
customOptions: MutableMap<String, Any>?
): NativeAdView {
val adView = LayoutInflater.from(context).inflate(R.layout.bottom_text_ad, null) as NativeAdView
adView.headlineView = adView.findViewById(R.id.ad_headline)
adView.callToActionView = adView.findViewById(R.id.ad_call_to_action)
(adView.headlineView as TextView).text = nativeAd.headline
(adView.callToActionView as Button).text = nativeAd.callToAction
adView.setNativeAd(nativeAd)
return adView
}
}

1. Add the com.google.android.gms.ads.APPLICATION_ID meta-data to your AndroidManifest.xml

<meta-data
  android:name="com.google.android.gms.ads.APPLICATION_ID"
  android:value="ca-app-pub-XXXXXXXXX"/>

2. implementation "com.google.android.gms:play-services-ads"

  • The latest version recommends Kotlin 2.1.0.
implementation "com.google.android.gms:play-services-ads:24.3.0"
package com.example.app
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
GoogleMobileAdsPlugin.registerNativeAdFactory(
flutterEngine,
"bottomTextAd",
BottomTextAdFactory(this),
)
}
override fun cleanUpFlutterEngine(flutterEngine: FlutterEngine) {
GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "bottomTextAd")
super.cleanUpFlutterEngine(flutterEngine)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment