Android

Add AdView to the layout

admin 2017.07.21 03:00 Views : 2608

The first step toward displaying a banner is to place AdView in the layout for the Activity or Fragment in which you'd like to display it. The easiest way to do this is to add one to the corresponding XML layout file. Here's an example that shows AdView at the bottom of an Activity:

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       
xmlns:tools="http://schemas.android.com/tools"
       
android:layout_height="match_parent"
       
android:layout_width="match_parent"
       
android:paddingLeft="@dimen/activity_horizontal_margin"
       
android:paddingRight="@dimen/activity_horizontal_margin"
       
android:paddingTop="@dimen/activity_vertical_margin"
       
android:paddingBottom="@dimen/activity_vertical_margin"
       
tools:context=".MainActivity">

       
<TextView android:text="@string/hello_world"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content" />

       
<com.google.android.gms.ads.AdView
           
xmlns:ads="http://schemas.android.com/apk/res-auto"
           
android:id="@+id/adView"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:layout_centerHorizontal="true"
           
android:layout_alignParentBottom="true"
           
ads:adSize="BANNER"
           
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
       
</com.google.android.gms.ads.AdView>


</RelativeLayout>

Note the following required attributes:

  • ads:adSize - Set this to the ad size you'd like to use (see the banner size section below for details).
  • ads:adUnitId - Set this to the unique identifier given to the ad unit in your app where ads are to be displayed. If you show banner ads in different activities, each would require an ad unit.
No. Subject Author Date Views
48 Website icon admin 2017.09.16 1942
47 Android Application Development admin 2017.07.28 1925
46 Load an ad admin 2017.07.21 2159
» Add AdView to the layout admin 2017.07.21 2608
44 Import the Mobile Ads SDK admin 2017.07.21 2158
43 AdMob - Import the Mobile Ads SDK admin 2017.07.21 858
42 Image Center admin 2017.07.21 865
41 US1800s admin 2017.07.03 879
40 Symbols And Holidays admin 2017.07.03 836
39 Rights And Responsibilities admin 2017.07.03 762
38 Recent American History admin 2017.07.03 834
37 Geography admin 2017.07.03 814
36 Colonial Period And Independence admin 2017.07.03 840
35 System of Government admin 2017.07.03 821
34 Principles of American Democracy admin 2017.07.02 773
33 package com.solatoz.joseph.uscitizenshiptest; admin 2017.07.01 804
32 Contact Layout admin 2017.06.06 1030
31 map api admin 2017.06.04 1919
30 Manifest admin 2017.06.04 902
29 Custom Swipe admin 2017.06.04 823