Thursday, 15 October 2015

Adding Ads to android app

1. Add a line to it like the one in bold above. This instructs gradle to include code from the play-services-ads artifact, which is found in the Google Repository.

build.gradle (excerpt)
...
dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:22.2.0'
        compile 'com.google.android.gms:play-services-ads:8.1.0'
    }
...



2. Add a new <string> tag as shown. Note that the Ad Unit ID provided above is just for testing. It allows you to retrieve a sample banner ad and make sure your implementation is correct.

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My Application</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
</resources>

An Ad Unit ID is a unique identifier given to the places in your app where ads are displayed. If you have an app with two activities, for example, each displaying a banner, you need two ad units, each with its own ID. Using live ads for testing is against google policy and this might lead to suspension of your google admob account

3. Add these to the XML: An additional namespace used for ads: http://schemas.android.com/apk/res-auto AND A new element for your AdView

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"          
    android:layout_height="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
        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="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

You'll be asked to provide layout_width and layout_height. You can set both to wrap_content. In the AdView tag, set the adSize to BANNER and the adUnitId to @string/banner_ad_unit_id.

4. Make these two changes:

  1. Import the AdRequest and AdView classes.
  2. Add code to find your AdView in the layout, create an AdRequest, and then load an ad into the AdView with it.

MainActivity.java (excerpt)

package ...

import ...
import ...
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends ActionBarActivity {

    ...

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

    ...

}
Once that's completed, you're finished. You now have a fully functional AdView in your app's main activity.


Adding ads into an existing app:
AdMob ads fall into two main categories: banners and interstitials. One of the first decisions you need to make as a publisher is which ads best fit the design and flow of your app.
Interstitials are full screen ads that cover the UI of their host app when displayed. They work best in apps that have occasional transitions from one task to the next, such as a game that pauses between levels.
Banner ads, on the other hand, occupy only a portion of the host app's UI, and stay visible for longer periods of time. They work best in layouts that can give up a small portion of their real estate without disrupting the user experience.
There are three main steps to incorporating an interstitial ad into one of your app's activities:
1.Construct an InterstitialAd object and set its ad unit ID
private InterstitialAd mInterstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    ...
    }
}

2.Request an ad

AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice("YOUR_DEVICE_HASH")
    .build();

mInterstitialAd.loadAd(adRequest);
To request an interstitial, the code builds an AdRequest and passes it to loadAd. Interstitial ads are loaded asynchronously, so it's important to make the request well in advance of the ad being needed.

3.Check that an ad is loaded, then display it

...
if (mInterstitialAd.isLoaded()) {
    mInterstitialAd.show();
}
...


Note that show() is not a blocking call. Your app will continue to execute even though the interstitial has control of the device screen. Be sure to pause any audio output or intense computation (such as a game loop) when showing an interstitial.


Banner ads, 
Banner ads which take up a portion of an app's screen real estate, are normally defined within an app's XML layout files, and then loaded within the corresponding activity or fragment's Java code.
There are two parts of this file that relate to banner ads:
  1. An additional namespace added to the RelativeLayout tag, which contains attributes used by theAdView.
  2. The AdView tag, which defines the position and characteristics of the banner ad within the layout.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="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
        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>

Loading the banner:
...
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice("YOUR_DEVICE_HASH")
    .build();
mAdView.loadAd(adRequest);
...
This code, which is also frequently placed in an activity or fragment's onCreate method, locates the banner ad present in the layout, builds an AdRequest, and uses it to load an ad.

Additional reading at: https://developers.google.com/admob/android/quick-start

No comments:

Post a Comment