Blog purpose for android basic example for android app developer. any query please my contact

Tuesday, 12 May 2020

android studio interstitial Ads Example

android studio interstitial Ads Example   

in this Article today learn android studio interstitial Ads Example. Interstitial ads are full-screen ads that spread the interface of their host app. They're regularly displayed at normal progress focuses on the progression of an app, for example, between exercises or during the delay between levels in a game. At the point when an app shows an interstitial advertisement, the client has the decision to either tap on the promotion and proceed to its goal or close it and come back to the app. for the more information visit the Android developer page 

 

Follow the method display interstitial full-Screen Ads :

// set the ad unit ID 

        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen)); 

 

        AdRequest adRequest = new AdRequest.Builder().build(); 

 

        // Load ads into Interstitial Ads 

        mInterstitialAd.loadAd(adRequest); 

 

        mInterstitialAd.setAdListener(new AdListener() { 

            public void onAdLoaded() { 

                showInterstitial(); 

            } 

        }); 

    } 

 

    private void showInterstitial() { 

        if (mInterstitialAd.isLoaded()) { 

            mInterstitialAd.show(); 

        } 

    } 

There Are the App id And Test Id Show the Android interstitial Ads.

App Id: ca-app-pub-3940256099942544~3347511713

Ad code: ca-app-pub-3940256099942544/1033173712


build.gradle:

implementation 'com.google.android.gms:play-services-ads:15.0.0'


Follow the Full Source code android studio interstitial Ads



1.create class MainActivity.java:


package com.akash.interstitial; 
import android.content.Intent;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
  
public class MainActivity extends AppCompatActivity {  
    Button button;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        button=(Button)findViewById(R.id.button);  
        button.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                   Intent intent = new Intent(MainActivity.this, InterstitialAdsActivity.class);  
                  startActivity(intent);  
  
            }  
        });  
    }  
}  


2.activity_main.xml:

<?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: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=".MainActivity">  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Interstitial Ads"  
        android:id="@+id/textView"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true" />  
  
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Start New Activity"  
        android:id="@+id/button"  
        android:layout_alignParentBottom="true"  
        android:layout_centerHorizontal="true" />  
</RelativeLayout>  


3.create activity InterstitialAdsActivity.java:


package com.akash.interstitial; 
import android.support.v7.app.AppCompatActivity;  
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;  
  
public class InterstitialAdsActivity extends AppCompatActivity {  
    InterstitialAd mInterstitialAd;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_interstitial_ads);  
        mInterstitialAd = new InterstitialAd(this);  
  
        // set the ad unit ID  
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));  
  
        AdRequest adRequest = new AdRequest.Builder().build();  
  
        // Load ads into Interstitial Ads  
        mInterstitialAd.loadAd(adRequest);  
  
        mInterstitialAd.setAdListener(new AdListener() {  
            public void onAdLoaded() {  
                showInterstitial();  
            }  
        });  
    }  
  
    private void showInterstitial() {  
        if (mInterstitialAd.isLoaded()) {  
            mInterstitialAd.show();  
        }  
    }  
  
  
}  


4.activity_interstitial_ads.xml:

<?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: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=.InterstitialAdsActivity">  
  
</RelativeLayout>  


5.String.xml:

<resources>  
    <string name="app_name">InterstitialAd Example</string>  
    <string name="interstitial_full_screen"> ca-app-pub-3940256099942544/1033173712</string>  
</resources>  


6.AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.akash.interstitial">  
  
    <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">  
        <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=".InterstitialAdsActivity"></activity>  
    </application>  
  
</manifest>  


7.dimens.xml:

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="nav_header_vertical_spacing">8dp</dimen>
    <dimen name="nav_header_height">176dp</dimen>
    <dimen name="fab_margin">16dp</dimen>
</resources>








No comments:

Post a Comment