Android Studio Admob Banner And Interstitial Tutorial

3 min


0

This tutorial will present you easy methods to implement Admob banner and interstitial advertisements utilizing Android Studio. They will aid you to make some cash on android. Interstitial are full display screen advertisements and must be displayed in a pure transition level i.e. throughout sport pause or when altering actions (i.e. earlier than exhibiting highscore).

[contact-form-7]

Install Dependencies

Before we begin let’s be certain we’ve all the mandatory conditions put in and prepared for use.

Download Google Repository package deal from Tools > Android > SDK Manager, which comprises the artifact Google Play SDK.

Android Studio Admob Banner And Interstitial Tutorial

Configure Gradle

Gradle is a construct system containing some nice options. It will assist us create a reference to google play companies. Edit the file beneath and add com.google.android.gms:play-services:7.0.0 to the dependency part.

File: construct.gradle(Module: app)

apply plugin: 'com.android.software'

android {
    compileSdkVersion 21
    constructToolsModel "21.1.2"

    defaultConfig {
        applicationId "com.ahotbrew.admobinterstitial"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        launch {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.professional'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', embody: ['*.jar'])
    compile 'com.android.assist:appcompat-v7:22.0.0'
    compile 'com.google.android.gms:play-services:7.0.0'
}

You might get a Sync Now warning in your Android Studio after including it. Click on the hyperlink as this may refresh your undertaking and embody the newest google play service library. Run the undertaking and ensure it compiles with out errors.

Create Ad

Start by including the two person permissions, meta-data and adActivity as proven beneath.

File: AndroidManifest.xml

<?xml model="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package deal="com.ahotbrew.admobinterstitial" >

    <!-- Include required permissions for Google Mobile Ads to run-->
    <uses-permission android:title="android.permission.INTERNET"/>
    <uses-permission android:title="android.permission.ACCESS_NETWORK_STATE"/>

    <software
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <!--This meta-data tag is required to make use of Google Play Services.-->
        <meta-data android:title="com.google.android.gms.version"
            android:worth="@integer/google_play_services_version" />
        <exercise
            android:title=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <motion android:title="android.intent.action.MAIN" />

                <class android:title="android.intent.category.LAUNCHER" />
            </intent-filter>
        </exercise>
        <!--Include the AdExercise configChanges and theme. -->
        <exercise android:title="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
    </software>

</manifest>

Create the format.

File: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:instruments="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:advertisements="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"
    instruments:context=".MainActivity">

    <com.google.android.gms.advertisements.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        advertisements:adSize="BANNER"
        advertisements:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.advertisements.AdView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textual content="Start Interstitial Add"
        android:id="@+id/newgame_button"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="58dp" />

</RelativeLayout>

The Admob id ca-app-pub-3940256099942544/6300978111 and ca-app-pub-3940256099942544/1033173712 are take a look at ids. Make certain you create your personal Admob advert unit and substitute the ids.

In this step we are going to present the Interstitial add on button click on and the banner on the underside of web page load.

File: MainActivity.xml

package deal com.ahotbrew.admobinterstitial;

import android.assist.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.advertisements.AdListener;
import com.google.android.gms.advertisements.AdRequest;
import com.google.android.gms.advertisements.AdView;
import com.google.android.gms.advertisements.InterstitialAd;

public class MainActivity extends ActionBarActivity {

    InterstitialAd mInterstitialAd;
    Button mNewGameButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        tremendous.onCreate(savedInstanceState);
        setContentView(R.format.activity_main);

        mNewGameButton = (Button) discoverViewById(R.id.newgame_button);

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

        mNewGameButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.present();
                } else {
                    //Begin Game, proceed with app
                }
            }
        });

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {

                //Begin Game, proceed with app
            }
        });

        AdView mAdView = (AdView) discoverViewById(R.id.advertView);
        AdRequest adRequest = new AdRequest.Builder().construct();
        mAdView.loadAd(adRequest);
        mInterstitialAd.loadAd(adRequest);
    }

}

Sit again, take a sip out of your sizzling brew and run your undertaking.

Don’t overlook to hitch or publication and get free android tutorials to take pleasure in together with your sizzling brew.

The submit Android Studio Admob Banner And Interstitial Tutorial appeared first on ahotbrew | Android growth tutorials.


What's Your Reaction?

hate hate
0
hate
confused confused
0
confused
fail fail
0
fail
fun fun
0
fun
geeky geeky
0
geeky
love love
0
love
lol lol
0
lol
omg omg
0
omg
win win
0
win

0 Comments

Your email address will not be published. Required fields are marked *

Choose A Format
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Poll
Voting to make decisions or determine opinions
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Meme
Upload your own images to make custom memes
Video
Youtube, Vimeo or Vine Embeds
Audio
Soundcloud or Mixcloud Embeds
Image
Photo or GIF
Gif
GIF format