Sunday, 10 July 2016

AdMob Ads in Android Studio



This tutorial will show you how to implement Admob banner and interstitial ads using Android Studio. They will help you to make some money on android. Interstitial are full screen ads and should be displayed in a new activity open.
 Banner ads are small screen ads.
Firstly check sdk your Google play services and Google Repository are installed or not if not install first install the Google play services and Google Repository

It will help us create a  google play services. Add only yellow color line added
Step 1:-
compile 'com.google.android.gms:play-services:8.4.0'
in build.gradle(module:app)
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
       applicationId "www.karantips.com.admob"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
         minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
 'proguard-rules.pro'
        }
   }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.google.android.gms:play-services:8.4.0'}


You may get a Sync Now Android Studio after adding this line. Click on the link as this will refresh your project and add the latest google play service library. Run the project.
Step 2:-Added two user permissions, meta data and activity
Your Androidmanifest.xml show this only yellow
 line added. Red color line is for new activity 
create coding
  <?xml version="1.0" encoding="utf-8"?> 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="www.karantips.com.admob">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data android:name="com.google.android.gms.version"  
android:value="@integer/google_play_services_version" />
        <activity android:name=".MainActivity">
            <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
        </activity>
        <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|
screenLayout|uiMode|screenSize|smallestScreenSize"
       android:theme="@android:style/Theme.Translucent" />
 <activity android:name=".SecondActivity"/>
    </application>
</manifest>

Step 3:-
Open activity_main.xml
Add these lines
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="www.karantips.com.admob.MainActivity">  
 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Greetings appear here.."
    android:id="@+id/greetings_text_view"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Open Second Activity"
    android:id="@+id/button"
    android:onClick="showGreetings"
    android:layout_below="@+id/greetings_text_view"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/greetings_text_view"
    android:layout_alignEnd="@+id/greetings_text_view"/>
    <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-5344662122110765/1809469731"/>
</RelativeLayout
 Note:- Replace this adunit id. This is trial id you can write your own id.
 only add Yellow color lines
Add these lines
Step 4:-
Open MainActivity.java
package www.karantips.com.admob;
 import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle;import android.view.View; 
import android.widget.Button; 
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends AppCompatActivity {
   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
       AdView adView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
         .setRequestAgent("android_studio:ad_template").build();
       adView.loadAd(adRequest);
    }
    public void showGreetings(View view) {
        String button_text = ((Button) view).getText().toString();
        if (button_text.equals("Open second Activity")) {
          Intent intent = new Intent(this, Second.class);
            startActivity(intent);
        }
   }
}
Step 5:- Open
SecondActivity.java
package www.karantips.com.admob;

import android.app.Activity;
import android.os.Bundle; 
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
 import com.google.android.gms.ads.InterstitialAd;/**
 * Created by Sahil on 7/1/2016.
 */public class SecondActivity extends Activity {
    private InterstitialAd interstitial;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        interstitial = new InterstitialAd(SecondActivity.this);
        interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        //   AdView adView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();
        // adView.loadAd(adRequest);
        interstitial.loadAd(adRequest);
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
               displayInterstitial();
            }
        });
    }
    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
   }
}

And lastly open String.xml
App------res---------value-------string.xml add these lines

<resources>
 <string name="app_name">Admob</string>
<string name="banner_ad_unit_id">ca-app-pub-5344662122110765/1809469731</string>
<string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string>
</resources>


Run the project firsly open your main activity. Banner ads are show. And Secondly you click the button open SecondActivity and Interstitial ads.
Note:- The Admob id ca-app-pub-3940256099942544/6300978111 and ca-app-pub-3940256099942544/1033173712 are test ids. Make sure you create your own Admob and replace the ids.



You like my post Please share with friends
You also learn my another posts of Android Studio Tutorial directly these links

No comments:

Post a Comment