I want to add this code to my java file:
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("http://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
}
But I don't know where. This is my java file:
package sherdle.donald.duck.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebPageLoader extends Activity
{
WebView webview;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setSupportMultipleWindows(true);
webview.getSettings().setPluginsEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webview.loadUrl("http://www.sherdle.com/apphosting/dd");
}
#Override
public void onBackPressed (){
if(webview.canGoBack()) webview.goBack();
else super.onBackPressed();
}
}
I'm new to android and I need much help for everything I do. Thanks for your help.
I've got already this now:
package sherdle.donald.duck.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebPageLoader extends Activity
{
WebView webview;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setSupportMultipleWindows(true);
webview.getSettings().setPluginsEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webview.setWebChromeClient(new WebChromeClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("http://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
webview.loadUrl("http://www.sherdle.com/apphosting/dd");
}
#Override
public void onBackPressed (){
if(webview.canGoBack()) webview.goBack();
else super.onBackPressed();
}
}
But I get this error on my LoadUrl line:
Multiple markers at this line
- Syntax error, insert "AssignmentOperator Expression" to complete
Expression
- Syntax error, insert ";" to complete FieldDeclaration
- Syntax error, insert ")" to complete MethodInvocation
- Syntax error, insert "}" to complete ClassBody
- Syntax error, insert ";" to complete Statement
- Syntax error on token(s), misplaced construct(s)
use in this way
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setSupportMultipleWindows(true);
webview.getSettings().setPluginsEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webview.setWebChromeClient(new WebChromeClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("http://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
webview.loadUrl("http://www.sherdle.com/apphosting/dd");
}
#Override
public void onBackPressed (){
if(webview.canGoBack()) webview.goBack();
else super.onBackPressed();
}
First make sure you have webview in your layout. Also make sure you have the following line in your manifest.
<uses-permission android:name="android.permission.INTERNET" />
Then you add that in your onCreate. It may not be the best, but it'll suffice.
Sorry, but how can you not understand where to insert that piece of code if you have exactly that same method already overridden? Look where it says:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
If you want to change the behaviour above, simply replace these two lines specified by that in the first code snippet.
I've got a feeling you don't really understand anonynous inner classes, as you also try to set a WebChromeClient twice - that doesn't really make sense. I suggest you do some reading on the matter.
Also, the shouldOverrideUrlLoading(...) method is defined by a WebViewClient, not a WebChromeClient. That's a case of simply consulting the documentation.
Related
Is their something i'm doing wrong? Do i have to include some lib?
I've tried adding the Youtube Api but that didn't help.
Edit#1: This is the WebView code
On some instance the Audio Playback works but instead of video it's just a rotating circle animation trying to load the video.
package com.PAKGN.Mizzy;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.webkit.WebSettings;
private WebView webView1;
private Toolbar toolbar;
private String title;
private String url;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
// this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
WebView myWebView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
title = getIntent().getExtras().getString("title");
url = getIntent().getExtras().getString("url");
getSupportActionBar().setTitle(title);
if (savedInstanceState != null) {
((WebView) findViewById(R.id.webView1)).restoreState(savedInstanceState);
} else {
webView1 = (WebView) findViewById(R.id.webView1);
webView1.getSettings().setJavaScriptEnabled(true);
webView1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
final Activity activity = this;
webView1.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
});
webView1.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webView1.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
runOnUiThread(new Runnable() {
#Override
public void run() {
// Code for WebView goes here
webView1.loadUrl(url);
}
});
}
}
#Override
protected void onSaveInstanceState(Bundle outState ){
((WebView) findViewById(R.id.webView1)).saveState(outState);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView1.canGoBack()) {
webView1.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
.
You need to enable javaScript.WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
EDIT:
You are setting the WebClient for webview twice.
`webView1.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
});`
You aren't really doing anything here thats different from the original implementation and this code isn't even running because you replace the webview with another webclient so delete this.
` runOnUiThread(new Runnable() {
#Override
public void run() {
// Code for WebView goes here
webView1.loadUrl(url);
}
});
`
Delete this too, just type webview1.loadUrl(url) no need for a runOnUiThread.
` webView1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Delete this too, its this line that is causing your issue. `
I tried to add a back button to my android webview but i've got this error:
The method onBackPressed() of type new WebViewClient(){} must override or implement a supertype method
This is my Java file:
package sherdle.donald.duck.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebPageLoader extends Activity
{
WebView webview;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public void onBackPressed() {
// do something on back.
return;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://example.com");
}
}
Thanks for your help.
According to the doc, there is no onBackPressed method in the WebViewClient class. Did you want to Override it on your activity ?
try smthing like this:
package sherdle.donald.duck.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebPageLoader extends Activity
{
WebView webView;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://example.com");
}
#Override
public void onBackPressed (){
if(webView.canGoBack()) webView.goBack();
else super.onBackPressed();
}
}
You have to override the onkeyDown() method in your Activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Try this
package sherdle.donald.duck.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebPageLoader extends Activity
{
WebView webview;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://example.com");
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack() == true)
webview.goBack();
else finish();
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
And this method should be override in Activty.
WebView webView = (WebView) findViewById(R.id.webview);
Change it to:
webView = (WebView) findViewById(R.id.webview);
and then use the second answer - works like a charm!
I'm trying to implement a loading dialog in my webview application but whatever I do it just won't work.
What I want is the following, everytime someone clicks a link in the webview the loading dialog has to popup like this:
(source: wordpress.com)
And when the loading is done it has to disappear. Can anyone show me how to do it in my own source? I've tried a lot of things but there's always an issue so I really don't know how to.
This is my WebviewActivity:
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebviewActivity extends MainActivity {
private WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
myWebView = (WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.requestFocus(View.FOCUS_DOWN);
myWebView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
}
#Override
public void onResume() {
super.onResume();
if ( isOnline() == true )
myWebView.loadUrl(webLink);
else if ( isOnline() == false )
showNoConnectionDialog();
}
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
}
Thank you very much for your help!!
You have to override the WebViewClient like this,
webview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, final String url) {
progressDialog.cancel();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressDialog.show();
}
});
webview.loadUrl(TwitterUrl);
This is my final solution:
myWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressDialog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
progressDialog.dismiss();
}
});
This can be possible with WebChromeClient. It does worked for me.
Code snippet :
progressDialog.show(); // Add before page load
webView.setWebChromeClient(new HelpWebChromeClient());
private class HelpWebChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if(newProgress == 100)
progressDialog.dismiss();
}
}
how can i open this url in web view in android
https://web150.secure-secure.co.uk/ftpeditor.net/core/homepage.php
try to use the chrome client:
vw = new MyWebView(this);
vw.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
vw.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
this is the completed code
package com.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.loadUrl("https://web150.secure-secure.co.uk/ftpeditor.net/core/homepage.php");
}
}
I want to load a HTML form into webview however it is not working for me and Im wondering if its even possible? This is the code I have.
Thanks
package com.timetable;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Timetable extends Activity {
WebView mWebView;
String html = "<html><body>";
Document docs;
public void main(String... args) {
try {
docs = Jsoup.connect("http://www.dcu.ie/timetables/search.shtml").get();
} catch (IOException e) {
e.printStackTrace();
}
Element table = docs.tagName("header-search");
Elements tables = table.select("form");
for (int i = 1; i < tables.size(); i ++)
{
html += tables.get(i).toString() ;
/*while (tables.get(i).text() == "")
i++;
html += "<tr>" + tables.get(i).toString();
while (tables.get(i+1).text() == "")
i++;
html +=tables.get(i+1).toString() + "</tr>";
i++;*/
}
html += "</html></body>";
}
public void onCreate(Bundle savedInstanceState) {
main();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new TimeClient());
mWebView.setInitialScale(1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadData(html, "text/html", "utf-8");
final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
private class TimeClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
also
don't do this : final Activity MyActivity = this;
instead use "MyActivity.this" inside WebChromeClient.onProgressChanged
also there is a gotcha with webView you cant do "loadData(data,mime,enc)" for some reason you need to use :
web.loadDataWithBaseURL("fake://host/path",htmldata.toString(), mimetype, encoding, "");
"fake://host/path" is a fake url but you could also use your homepage or something.
so something like :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new TimeClient());
mWebView.setInitialScale(1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
//mWebView.loadData(html, "text/html", "utf-8");
//final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
MyActivity.this.setTitle(R.string.app_name);
}
});
}
public void onStart() {
main();
mWebView.loadData("fake://host/path",html, "text/html", "utf-8");
}
public void onStop() {
// some JSoup disconnect code
}
then the webView will load every time the app is launched.
move view.loadUrl(url); to your onStart method.
if you was a loading icon, override onPageStarted & onPageFinished in WebViewClient. then you can use WebChromeClient.onProgressChanged to update your progress bar.