Placing advertisement in self created app is always good. Google Admob Banner adv will help you to place banner adv on your activities. So let’s focus on how can we implement this in our application. This will help you to place Banner adv in your Android application using Java or Kotlin.
- Google Admob Account – requires only for real case, for testing not require, we are using demo Adv IDs here.
- Basic Programming Knowledge in Java or Kotlin
- Android Emulator or Phone – To test our app.
Add Dependency
To work with google adv, add the below dependencies in your gradle file.
implementation 'com.google.android.gms:play-services-ads:21.3.0'
Add Banner adv in XML Layout
Add below code in your Layout XML file as displaying on the bottom of your screen.
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/bannerAdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/bannerAdv"
android:layout_alignParentBottom="true" />
also add the below code in your string.xml file. Replace the below demo banner id with your banner id provided banner ID before your upload your application in to Google Play Store.
<string name="bannerAdv">ca-app-pub-3940256099942544/6300978111 </string>
Modify Android Manifest.xml File
As per google, add below piece of app id information in androidmanifest.xml file between <application></application> tabs. replace with your app-id value provided from Admob before uploading you app to google play store.
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
Internet permission
Also don’t forget to add internet permission otherwise your app won’t show adv as it cannot communicate to internet. place the below code before application tab in the same androidmanifest.xml file.
<!-- Permissions of internet -->
<uses-permission android:name="android.permission.INTERNET" />
Code to Display banner Adv
If all the above things are ready, it is the time to add some code in your project to display the banner adv.
Java Sample Code
Place the below code in your java class file in you are using java as your preferred language.
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//for banner adv
AdView mBannerAdView = findViewById(R.id.bannerAdViews);
AdRequest bannerAdRequest = new AdRequest.Builder().build();
mBannerAdView.loadAd(bannerAdRequest);
}
}
Kotlin Sample Code
Place the below code in your kotlin class file in you are using kotlin as your preferred language.import com.google.android.gms.ads.*
class MainActivity : AppCompatActivity() {
lateinit var adViewBanner: AdView
lateinit var adRequestBanner: AdRequest
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// For banner Adv
MobileAds.initialize(this)
adViewBanner = findViewById(R.id.bannerAdView)
adRequestBanner = AdRequest.Builder().build()
adViewBanner.loadAd(adRequestBanner)
}
}
Run the project and see how google banner adv is displaying in your app.