Reputation: 498
i am a php/web/mysql/css/js developer and new to both, java and android. My first webview works like a charm, but i canot figure out why my admob is not working at all. i tried several tutorials from here and from the www so far, but without success. i am sure any of you geeks will find the issue immediately, so thank in advance. Will add the relevant code to this topic below, please let me know what is wrong here on my side. If you need further sources, i can add it here as well.
thats what i have so far:
MainActivity
package smartmedia.de.mywebviewapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import static smartmedia.de.mywebviewapp.AnalyticsWebInterface.TAG;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends Activity {
private WebView mWebView;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.activity_main_webview);
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
mWebView.addJavascriptInterface(new MyTtsTalker(this), "MyTtsTalker");
mWebView.addJavascriptInterface(new AnalyticsWebInterface(this), TAG);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(false);
webSettings.setDefaultTextEncodingName("UTF-8");
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setAppCacheEnabled(false);
webSettings.setDatabaseEnabled(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.loadUrl("file:///android_asset/index.html");
MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
}
@Override
public void onBackPressed()
{
if(mWebView.canGoBack())
{
mWebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
activity_main.xml
<?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"
android:orientation="vertical"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/adView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<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="50dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.google.gms:google-services:3.1.2'
classpath 'com.google.firebase:firebase-ads:15.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Upvotes: 1
Views: 4104
Reputation: 1789
Change you xml like this:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/activity_main_webview"
android:layout_above="@id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
ads:adSize="BANNER"
ads:adUnitId="@string/footer_ad_id"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<!--
In the above adView, change height from wrap_content to some particular
size, 30dp, for example to see if you get the desired layout.
Even if the ad doesn't load, you know the layout is right because there
will be a black color where the ad should load.
-->
</RelativeLayout>
Upvotes: 2
Reputation: 538
You need to change your xml file like below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/footer_ad_id"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
</com.google.android.gms.ads.AdView>
</LinearLayout>
Upvotes: 0