0

I want to show Interstitial ad on my webview android app but I am unable to implement it.

  • I have an webview app which is fully online, with no offline pages or any button or menu.

  • I don't want to show Interstitial ad on starting or closing app.

  • Ad should be shown when user browse my website using that webview app.
  • I thought to use Interstitial ad after x minutes, but I have learned Interstitial ad after x minutes is not a good idea from here and here.
  • Then how can I show Interstitial ad on my app as I told, I don't have any button or menu but only a website to browse.
  • Please suggest me how can I use Interstitial ad on my webview app.
  • I tried and successfully implement BANNER ad. with the help of stackoverflow question-answer and Google (code given below) but failed to implement Interstitial ad by any means
  • I'm very much new in this sector so please suggest me with detail process means where put code etc.

Activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.xxxx.yyyyy.MainActivity"
    android:layout_gravity="center"
    android:id="@+id/fl">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/wv"/>
    <ProgressBar
        android:id="@+id/progressBar"
        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-xxxxx/xxxxxx">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

Mainactivity java

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;

MobileAds.initialize(this, "xxxxxx~xxxxxx");
        AdView adView = (AdView)findViewById(R.id.adView);

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("xxxxxxx")
                .build();
        adView.loadAd(adRequest);

using firebase (build.gradle)

compile 'com.google.firebase:firebase-ads:10.2.6'
Suhayl SH
  • 1,213
  • 1
  • 11
  • 16
user3137451
  • 181
  • 1
  • 2
  • 16
  • I dont think interstitial ads gives you a custom click handling mechanism. But if you want to handle the user clicks, you should look NativeCustomTemplateAds https://developers.google.com/mobile-ads-sdk/docs/dfp/android/native – Ezio May 30 '17 at 05:51

3 Answers3

0

The best option available for you is to set a webview client to your webview and load the Ad when user open some specific url by using shouldOverrideUrlLoading method

 mWebView.setWebViewClient(new WebViewClient() {

   @Override
 public boolean shouldOverrideUrlLoading(WebView view, String urlNewString)     {

 //urlNewString is  url for loading Ad then load Ad

return false;
}

@Override
public void onPageStarted(WebView view, String url) {

    }

});
Manohar
  • 22,116
  • 9
  • 108
  • 144
0

Yes you can show interstitial from your web app when clicking on button using JavascriptInterface in Android WebView.

  1. First add addJavascriptInterface to your webview.
  2. Create a calss for JavascriptInterface
  3. create a method for showing ad say "showAd()" inside you Acitvity
  4. create a methos in your javascriptInterface class and call your showAd() to show ad.

For reference see this How to call admob Interstitial ad from android webview JavascriptInterface

Md Junaid Alam
  • 1,131
  • 13
  • 20
0

on the activity ur showing the webview use android lifecycle :-). when you minimise ur app the intertitial ads will load and when app will be opened the ads will be shows.below is an example i m doing this way in my apps for showing ads.... hope this helps

@Override
protected void onDestroy() {
    super.onDestroy();


}

@Override
protected void onStop() {
    super.onStop();
  loadads(); 

}

@Override
protected void onResume() {
    super.onResume();
   showads();   
}

@Override
protected void onPause() {
    super.onPause();

}
Jasbin karki
  • 127
  • 2
  • 8