0

I have tried making an app for my website with a webview and progressbar, the visibility of the webview remains hidden till the page loads. Now I have tried adding google admob banner ad at the botton and it isn't showing up, I am using a relativelayout and the ad shows in preview of xml, but not in app.

The 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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<WebView
    android:id="@+id/activity_main_webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:visibility="gone" />

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:background="#000000"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3467064285652442/5673227109">
</com.google.android.gms.ads.AdView>

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    app:layout_constraintStart_toStartOf="@+id/activity_main_webview"
    app:layout_constraintTop_toTopOf="@+id/activity_main_webview" />



    </RelativeLayout>

The MainActivity.java:

package com.dhruv.spadebee;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ProgressBar;
import android.util.Log;
import android.view.MenuItem;
import android.view.Window;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import android.view.View;

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    String ShowOrHideWebViewInitialUse = "show";
    private WebView mWebView;
    private ProgressBar spinner;
    private AdView mAdView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        mAdView.setVisibility(View.VISIBLE);
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.custom_toolbar);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#080808")));
        final ImageView img = (ImageView) findViewById(R.id.toolbar_top);
        img.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Creating the instance of PopupMenu
                PopupMenu popup = new PopupMenu(MainActivity.this, img);
                //Inflating the Popup using xml file
                popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

                //registering popup with OnMenuItemClickListener
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.one:
                                mWebView.loadUrl("https://spadebee.com/");
                                break;
                            case R.id.two:
                                mWebView.loadUrl("https://spadebee.com/category/general/");
                                break;

                            case R.id.three:
                                mWebView.loadUrl("https://spadebee.com/category/programming/");
                                break;

                            case R.id.four:
                                mWebView.loadUrl("https://spadebee.com/category/gaming/");
                                break;

                            case R.id.five:
                                mWebView.loadUrl("https://spadebee.com/category/android/");
                                break;

                            case R.id.six:
                                mWebView.loadUrl("https://spadebee.com/about");
                                break;

                        }

                        return true;
                    }
                });
                popup.show();//showing popup menu
            }
        });//closing the setOnClickListener method






        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
                .init();
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        // Stop local links and redirects from opening in browser instead of WebView
        spinner = (ProgressBar)findViewById(R.id.progressBar1);
        mWebView.setWebViewClient(new MyAppWebViewClient() {
           @Override
            public void onPageStarted(WebView webview, String url, Bitmap favicon) {
                if (ShowOrHideWebViewInitialUse.equals("show")) {
                    webview.setVisibility(webview.INVISIBLE);
                }
            }


            @Override
            public void onPageFinished(WebView view, String url)
            {
                mWebView.loadUrl("javascript:(function() { " +
                        "var head = document.getElementsByClassName('custom-header')[0].style.display='none'; " +
                        "})()");
                mWebView.loadUrl("javascript:(function() { " +
                        "var head = document.getElementsByClassName('main-navigation')[0].style.display='none'; " +
                        "})()");
                spinner.setVisibility(View.GONE);

                mWebView.setVisibility(WebView.VISIBLE);
                super.onPageFinished(view, url);
            }
        });
        mWebView.loadUrl("https://spadebee.com/");
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
    private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
        // This fires when a notification is opened by tapping on it.
        @Override
        public void notificationOpened(OSNotificationOpenResult result) {

            Log.i("OSNotificationPayload", "result.notification.payload.toJSONObject().toString(): " + result.notification.payload.toJSONObject().toString());
            mWebView = (WebView) findViewById(R.id.activity_main_webview);
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            String appURL = result.notification.payload.launchURL;
            Log.i("appURL", appURL);
            mWebView.loadUrl("https://spadebee.com");
            mWebView.setWebViewClient(new MyAppWebViewClient());
        }
    }
    public class jparser {
        public void main(String[] args) throws IOException {
            Validate.isTrue(args.length == 1, "usage: supply url to fetch");
            String url = args[0];
            Document doc = Jsoup.connect(url).get();
            Elements links = doc.select("a[href].more-link");
            Elements media = doc.select("img");
            Elements summary = doc.select("div.entry-summary");

        }
    }
}

The preview:

Android Studio preview

Any help as to why it isn't working will be appreciated, I have tried going through all the posts here, adding progresbar and webview in a linear layout, setting adviews visibility to visible but nothing works.

1 Answers1

1

Locking at your log output the last line

I/Ads: Ad failed to load : 3

states that there went something wrong while loading the ad, so there probably is nothing to show?

As stated in this post failed to load ad : 3 regarding error "3":

If you are getting this error, then your code is correct. The issue is that AdMob does not always have an ad to return for every request.

So maybe this is just a temporary problem, have you tried your app on a different device or after a little break? Also you could try to use the android sample ad ids https://developers.google.com/admob/android/test-ads#sample_ad_units they usually work

SyntaX
  • 328
  • 2
  • 8
  • Thanks a lot! The test ads work successfully, might be just an issue by Google's end as my banner ads code doesn't show anything. – Dhruv Gera May 25 '20 at 14:27