ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Communications»
  • Smartphones»
  • Android Phones

How to Show Admob Ads in Android Apps

Updated on July 18, 2014

For monetizing android apps, you can show mobile adds in your application. Though, there are a number of ways you can add advertisement in your apps, this article will focus on adding ads in the XML layouts. To show ads in XML layout, you need to follow the following steps:

  1. Sign up for Admob or any other mobile ad program. If you sign up for admob they will give you the code for each ad you create. You just have to add this ad code in your xml file.
  2. Download Google Play Mobile SDK and add it to your project.

If you already have an admob account, create an ad and copy the ad code, which you will have to add in your XML file. Remember that there are serveral ad programs such as "DoubleClick For Publishers", Ad Exchange, search ads for mobile. So, if you want to show ads in your apps, admob is not the only solution. But at the beginning, you can start with admob because they have one of the largest ad inventories.

how to add android ad in android apps
how to add android ad in android apps | Source

Before installing the SDK make sure you meet the following requirements.

In your AndroidManifest.xml file, you need to set the android:minSdkVersion level to a minimum of 9.

android:minSdkVersion to at least 9 in your AndroidManifest.xml

In the project properties file set the target level to android-13.If it is already set to 13, you do not have to do modify anything. The project.propertifes file is located in the ‘res’ folder of your android project.

How to install Google Mobile Ads SDK?

  1. To install Google Mobile ads SDK, you need to download Google Play services in your Eclipse. If you already have it in your eclipse you just have to add it in your android project. If you do not have Google paly service, follow the steps given below:
  2. In your eclipse, click on “window” and then click on “Android SDK Manager”
  3. Next click on ‘Extra’ and select “Google Play Services”. Click on the “install package” to start installing the Google Play Services.

Note: Remember that Google play services contains the Goolge_play_service_lib that you will need to configure in your project, which you will come to know shortly.

Once installation completes, go to the following folders:

  1. Go to adt-bundle-windows-x86-20140321\sdk\extras\google and copy the “google play services” folder that you have downloaded earlier using the SDK manager.
  2. Now, in your eclipse, paste the “google play services” folder in the ‘libs’ folder of your android project.
  3. Now, click on ‘File” and then ‘import’. Select ‘Android’ and then select
    “existing android code into workspace”. Click ‘Next’ and browse to find the location of the libs folder in your project workspace where pasted the “google play services”. Just select the ‘libs’ folder, not need to select other files inside it. Click ‘OK’ .Next, select the folder that has Google_play_services_lib and deselect all other folders and finish the installation. Now, your project has the necessary mobile SDK that helps to show ads in your apps.
  4. The next step after completing the installation is to add the Google play service to your project. Right click on your project, select ‘Android’ and click ‘ADD’ under the library and then select “google_play_services_lib” and click ‘OK’ and ‘Apply.’

AndroidManifest.xml file

You need to add a few lines of code in the Maniest.xml file:

  1. Add meta data
  2. Add Ad-activity
  3. Add permission

Meta data:

  <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>


Ad-activity:

<activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

Add Network permission: add the following codes just before tag in the AndroidManifest.XML file.

<uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

You can have a look at Google’s android developer’s site for more details about the above mentioned codes: https://developers.google.com/mobile-ads-sdk/docs/


Codes for JAVA class file.

The following JAVA codes need to be added in the MainActivity.Java or any other java file that will load the xml file. Normally, you need to add this code right after the “ setContentView(R.layout.main);

AdView adView = (AdView)this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

Codes for XML file: suppose you want to show the banner ads in your apps. You have to add the following code in the xml layout file that will be loaded when

<com.google.android.gms.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="MY_AD_UNIT_ID"
                         ads:adSize="BANNER"/>

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: "https://hubpages.com/privacy-policy#gdpr"

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)