Reputation: 15
In my simple android application I have a webview filling up half of the screen. All I want it to do currently is load a webpage. The problem is whenever I run my application it says webpage not available. Then if I click on the link (https://www.google.com/), where it says the webpage at https://www.google.com/ might be temporarily down or it may have moved permanently to a new address it will open the browser and go strait to the website without any problems. Here are some pieces of code:
MainActivity.java:
package com.example.tscschools;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("https://www.google.com/");
}
}
activity_main.xml:
<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" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="210dp" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.jackson"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.android.jackson.MainActivity"
android:label="@string/app_name"
android:hardwareAccelerated="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I heard somewhere when running android 4.0+ or 4.4(Im running 4.4.2), it may cause some problems with the webview? I'm not sure if thats what is causing the problem or not, but I thought I might throw it out there. I really hope whoever reads this can fix it! Thanks!
Upvotes: 1
Views: 1754
Reputation: 1153
You should implement a WebViewClient and setContentView for your browser like this web.setWebViewClient(new myWebClient()); web.setWebChromeClient(new WebChromeClient()); setContentView(web);
package com.example.yourappname;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
WebView web;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webView1);
web = new WebView(this);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.yoururlhere.com");
web.setWebViewClient(new myWebClient());
web.setWebChromeClient(new WebChromeClient());
setContentView(web);
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
//flip screen not loading again
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(web.canGoBack()){
web.goBack();
}
else
{
backButtonHandler();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
public void backButtonHandler() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
MainActivity.this);
// Setting Dialog Title
// Setting Dialog Message
alertDialog.setTitle("Alert Dialog Title Here");
alertDialog.setIcon(R.drawable.dialog_icon);
alertDialog.setMessage("Exit Now?");
// Setting Icon to Dialog
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
}
Upvotes: 0
Reputation: 1153
You should implement a "WebViewClient" and "setContentView" inside your onCreate(Bundle savedInstanceState) above or below "webView.loadUrl", like this:
web.setWebViewClient(new myWebClient());
web.setWebChromeClient(new WebChromeClient());
setContentView(web);
and you are done. All the best..
Upvotes: 0
Reputation: 47807
You need to implement WebViewClient
like:
webView.loadUrl("https://www.google.com/");
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
});
In your case you have to implement WebChromeClient
and WebChromeClient
is used to handle a JavaScript
events in Android App which are produced by WebView
. The examples of such events are :
onCloseWindow
onProgressChanged
onJsAlert
onJsConfirm
onJsPrompt
onJsTimeout
Upvotes: 1
Reputation: 447
From your code I can see you have not defined WebViewClient for your webview. You have just added WebChromeClient.
Add this line in your oncreate()
webView.setWebViewClient(new WebViewClient());
Upvotes: 0
Reputation: 8371
Try changing your webView.setWebChromeClient(new WebChromeClient());
line with the following to make sure that you aren't getting an SSL error:
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
Log.w("webView", "Encountered an SSL error");
// handler.proceed();
}
});
You can ignore SSL errors by uncommenting the handler.proceed();
line, but it would be better to actually resolve the SSL issue.
Upvotes: 0