Banner ads are a great way to monetize an app. They can also be used to promote other apps or content on your website. In this tutorial, we will show you how to show a banner ad in multiple activities and fragments using only six lines of code.
The first step is to create a banner ad in your app. You can use any ad network you like, such as Google AdMob. Once you have created the ad, you need to get the ad unit ID. This is a unique identifier for your ad that the ad network will use to track impressions and clicks.
Next, you need to create a layout for the banner ad. The layout should have a width of 320 pixels and a height of 50 pixels. It should also have a LinearLayout as a child. The LinearLayout should have a vertical orientation and its orientation should be set to “vertical”.
The layout should also have an ImageView as a child. The ImageView should have a width of 320 pixels and a height of 50 pixels. It should also have a src attribute that points to the ad unit ID you obtained in the first step.
Here is an example of the layout you should use:
Now that you have created the layout, you need to add it to your activity or fragment. You can do this by adding the following code to your activity or fragment:
BannerView bannerView = (BannerView) findViewById(R.id.banner_view); bannerView.setAdUnitId(adUnitId);
You can also add the banner view to the layout file for your activity or fragment. This is optional, but it is a good idea to do so. This will ensure that the banner view is always visible, regardless of the device or screen size.
Here is an example of how you can add the banner view to the layout file