ads_header

How To Add Admob in Android Studio

How To Add Admob in Android Studio



Preview




Hello friends, in this post I will discuss about how to display admob into project in Android Studio.

Admob is a powerful monetization platform for apps, which can help you maximize revenue from ads and in-app purchases.

First, open the SDK manager in Android Studio and make sure you have installed Google Repository as shown below


After that, create a new project and open the dependencies section and enter the following libraries

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:appcompat-v7:25.3.1'
  compile 'com.android.support:design:25.3.1'
  compile 'com.google.android.gms:play-services-ads:7.5.0'  
}

Next, create a layout and name it activity_main and enter the following code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context="com.example.ndiappink.myapplication.MainActivity">

  <Button
      android:layout_width="wrap_content"
      android:id="@+id/banner"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:text="View Banner Ads"
      android:layout_height="wrap_content"/>

  <Button
      android:layout_width="wrap_content"
      android:layout_below="@+id/banner"
      android:id="@+id/inter"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      android:text="View Interstitial Ads"
      android:layout_height="wrap_content"/>
</RelativeLayout>

Next, create another layout with the name banner and enter the following code to display the ad with the Banner type

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    <com.google.android.gms.ads.AdView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/adview"
         android:layout_centerHorizontal="true"
         android:layout_alignParentBottom="true"
         android:adSize="BANNER"
         android:adUnitId="ca-app-pub-3940256099942544/6300978111">                   
    </com.google.android.gms.ads.AdView>
</RelativeLayout>





Next, create a layout again with the name interstitial and enter the following code to display ads with Interstitial type (Full Screen)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

</RelativeLayout>

Now, create a new class and name the banner and enter the following code

import android.os.Bundle;
import android.supportv7.app.AppCompatActivity;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class banner extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.banner);
        Adview adView = (Adview)findViewById(R.id.adview);
        AdRequest adRequest = new AdRequest.Builder().build();   
        adView.loadAd(adRequest);
    }
}

Next, create a new class again and name it Interstitial and enter the following code

import android.os.Bundle;
import android.supportv7.app.AppCompatActivity;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.AdListener;

public class Interstitial extends AppCompatActivity{
    private InterstitialAd interstitial;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.interstitial);
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");    
        AdRequest adRequest = new AdRequest.Builder().build();
        interstitial.loadAd(adRequest);

        interstitial.setAdListener(new AdListener() {
             @Override
             public void onAdClosed() {
                  displayInterstitial();
             }
        });
     }
     public void displayInterstitial(){
          if (interstitial.isLoaded()){
              interstitial.show();
          }
     }
}

Mobile Landing Page

FluidApp - Responsive Mobile App WordPress Theme

FluidApp is a sleek, responsive WordPress theme for Mobile, iPad and Tablet apps.

Coded using the latest HTML5 and CSS3 standards, choose from iPhone, Android, Windows,
Nexus and/or Blackberry and simply upload your own screenshots! FluidApp includes the light and dark version,
custom jQuery homepage slider, 8 unique page layouts, screenshot gallery, change-log page, icon picker,
blog and real-time Google font picker. All this with awesome support!



Get it Now

Now go to MainActivity.class and enter the following code

import android.os.Bundle;
import android.supportv7.app.AppCompatActivity;
import android.content.Intent;
import android.widget.Button;


public class MainActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button Banner = (Button)findViewById(R.id.banner);
        Button Inter = (Button)findViewById(R.id.inter);
        
        Banner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent inten = new Intent(MainActivity.this, banner.class);
                startActivity(inten);
            }
        });
        Inter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent inten = new Intent(MainActivity.this, Interstitial.class);          
                startActivity(inten);
            }
        });

     }
}

And finally, do not forget to add activity from the banner and interstitial classes in Manifest.xml and add access to the Internet

<activity android:name=".banner"/>
<activity android:name=".Interstitial"/>

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

For more details, please watch the tutorial video below



Powered by Blogger.