How to navigate webpage history in webview - android

I have webview included in my app. I want to navigate webpage history, but I don`t know how to do that. Please help me. My Main.java below:
package com.abc.test;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView wv = (WebView)findViewById(R.id.webview);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv.loadUrl("http://www.google.com");
}
}

Here is the code
public void goBackInWebView(){
WebBackForwardList history = webView.copyBackForwardList();
int index = -1;
String url = null;
while (webView.canGoBackOrForward(index)) {
if (!history.getItemAtIndex(history.getCurrentIndex() + index).getUrl().equals("about:blank")) {
webView.goBackOrForward(index);
url = history.getItemAtIndex(-index).getUrl();
Log.e("tag","first non empty" + url);
break;
}
index --;
}
// no history found that is not empty
if (url == null) {
finish();
}
}

Related

Making a News reading app in Android Studio (WebView). but when ever i click a video in a selected article it does not play

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. `

WebView based app, YouTube videos does not have full screen button

I have a WebView based app that shows a WordPress website. I have in my website a few YouTube videos, and the problem is YouTube videos does not have full screen button. I looked for an answer to this and didn't find anything on the web. Can someone help me? how can I make the YouTube videos have the option to play in full screen from my WebView?
Here is my MainActivity code:
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.delitestudio.pushnotifications.PushNotifications;
import com.loopj.android.http.*;
import cz.msebera.android.httpclient.*;
import cz.msebera.android.httpclient.impl.entity.StrictContentLengthStrategy;
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final WebChromeClient webChromeClient = new WebChromeClient();
private static final WebViewClient webViewClient = new WebViewClient();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url;
Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey("fromPush")) {
url = "http://www.paotiptipon.co.il/%d7%9b%d7%a0%d7%99%d7%a1%d7%aa-%d7%94%d7%95%d7%a8%d7%99%d7%9d-2/%d7%a4%d7%95%d7%a1%d7%98%d7%99%d7%9d/";
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl(url);
} else {
url = "http://paotiptipon.co.il/";
}
WebView myWebView = (WebView) this.findViewById(R.id.webView);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebChromeClient(webChromeClient);
myWebView.setWebViewClient(webViewClient);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setAllowContentAccess(true);
myWebView.getSettings().setDatabaseEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.loadUrl(url);
myWebView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack())
webView.goBack();
break;
}
}
return false;
}
});
notificationsButtonClicked();
newNotificationButtonClicked();
calendarButtonClicked();
final PushNotifications pn = new PushNotifications(this);
if (pn.getToken() == null || pn.isExpired()) {
pn.refreshToken("http://www.paotiptipon.co.il/pnfw/register/",
"35299935473");
}
// Bundle extras = getIntent().getExtras();
// if (extras != null) {
// int id = extras.getInt(PushNotifications.ID);
// String title = extras.getString(PushNotifications.TITLE);
// System.out.println(id + title);
// }
}
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
mIntentReceiver.abortBroadcast();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("התראה חדשה!");
alertDialog.setMessage("יש התראה חדשה במרכז ההתראות! לחצו על אישור כדי לראות אותה!");
// Setting OK Button
alertDialog.setPositiveButton("אישור",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String url = "http://www.paotiptipon.co.il/%d7%9b%d7%a0%d7%99%d7%a1%d7%aa-%d7%94%d7%95%d7%a8%d7%99%d7%9d-2/%d7%a4%d7%95%d7%a1%d7%98%d7%99%d7%9d/";
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl(url);
}
});
// Setting CANCEL Button
alertDialog.setNegativeButton("ביטול",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialog.show();
}
};
private void notificationsButtonClicked() {
Button notifications = (Button) findViewById(R.id.notifications);
notifications.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.paotiptipon.co.il/%d7%9b%d7%a0%d7%99%d7%a1%d7%aa-%d7%94%d7%95%d7%a8%d7%99%d7%9d-2/%d7%a4%d7%95%d7%a1%d7%98%d7%99%d7%9d/";
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl(url);
}
});
}
private void newNotificationButtonClicked() {
Button newNotification = (Button) findViewById(R.id.newNotification);
newNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.paotiptipon.co.il/wp-admin/post-new.php";
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl(url);
}
});
}
private void calendarButtonClicked() {
Button calendar = (Button) findViewById(R.id.calendar);
calendar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.paotiptipon.co.il/events/";
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl(url);
}
});
}
#Override
protected void onResume() {
super.onResume();
registerReceiver(mIntentReceiver, PushNotifications.getIntentFilter(this));
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mIntentReceiver);
}
}
first of all thanks for those of you who tried to help.
I found an answer to my question, and I would like to share it here so if anyone needs this it will be here.
I used this link:
Playing HTML5 video on fullscreen in android webview
I just downloaded the example project from there and implemented the code there in my project. The man who wrote those classes is awesome and wrote a really good tutorial on this and it fixed my problem. :)

On Button Click Get contents from a WebView textarea Android

I've searched on google and stackoverflow but cannot find any solutions relevant to Android API 17 and above.
I like to retrieve data from a webview on Button Click.
I like to achieve something similar to what ios has:
myText.stringByEvaluatingJavaScriptFromString
("document.getElementById('chapter-description').value")
my source code is:
package com.xyz.webviewtest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
public class MainActivity4 extends AppCompatActivity {
TextView textView;
#SuppressLint("JavascriptInterface")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
webview.loadUrl("javascript:window.HtmlViewer.showHTML" +
"('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
}
});
// new MyJavaScriptInterface(this).showHTML("HtmlViewer");
webview.loadUrl("http://www.xsoftech.com");
}
class MyJavaScriptInterface {
private Context ctx;
MyJavaScriptInterface(Context ctx) {
this.ctx = ctx;
}
public void showHTML(String html) {
new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
.setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
}
}
}
http://developer.android.com/ states that the constructor is deprecated
This constructor was deprecated in API level 17. Private browsing is
no longer supported directly via WebView and will be removed in a
future release. Prefer using WebSettings, WebViewDatabase,
CookieManager and WebStorage for fine-grained control of privacy data.
Webview have textarea so i can write something after writing i want able to get data on button click. Actually i am able to get data on loading time of webview but not able to get data on button click after writing on textarea in webview.
I found a possible solution, but I am not able to get any data on button click.
The solution is:
package example.xsoftech.sample;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
public class Main2 extends Activity {
WebView webview1;
public static String data;
Button button;
String sUrl = "http://www.facebook.com/";
// String sUrl = "http://www.xsoftech.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
webview1 = (WebView)findViewById(R.id.myWebview);
webview1.getSettings().setJavaScriptEnabled(true);
webview1.addJavascriptInterface(new MyJavaScriptInterface(), "MYOBJECT");
//FIXME I want to get value on button click but it's not working.So plz give me solution for work it?
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(Main2.this, "click is done: "+data, Toast.LENGTH_SHORT).show();
data= "";
webview1.loadUrl(sUrl);
}
});
webview1.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("lifecyle", "shouldOverrideUrlLoading");
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("lifecyle", "onpagefinished");
StringBuilder sb = new StringBuilder();
sb.append("document.getElementsByTagName('form')[0].onsubmit = function () {");
sb.append("var objPWD, objAccount;var str = '';");
sb.append("var inputs = document.getElementsByTagName('input');");
sb.append("for (var i = 0; i < inputs.length; i++) {");
sb.append("if (inputs[i].type.toLowerCase() === 'password') {objPWD = inputs[i];}");
sb.append("else if (inputs[i].name.toLowerCase() === 'email') {objAccount = inputs[i];}");
sb.append("}");
sb.append("if (objAccount != null) {str += objAccount.value;}");
sb.append("if (objPWD != null) { str += ' , ' + objPWD.value;}");
sb.append("window.MYOBJECT.processHTML(str);");
sb.append("return true;");
sb.append("};");
view.loadUrl("javascript:" + sb.toString());
}
});
webview1.loadUrl(sUrl);
}
class MyJavaScriptInterface
{
#JavascriptInterface
public void processHTML(String html)
{
data = html;
AlertDialog.Builder builder = new AlertDialog.Builder(Main2.this);
builder.setTitle("AlertDialog from app")
.setMessage(html)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setCancelable(false).show();
}
}
}
any help is appreciated.
I am really happy to giving answer
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
final WebView browser = (WebView)findViewById(R.id.myWebview);
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);
/* Register a new JavaScript interface called HTMLOUT */
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
// This call inject JavaScript into the page which just finished loading.
browser.loadUrl("javascript:window.HTMLOUT.processHTML(document.getElementsByTagName('textarea')[0].innerHTML);");
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
browser.loadUrl("javascript:window.HTMLOUT.processHTML(document.getElementById('chapter-description').value);");
}
});
/* load a web page */
browser.loadUrl(sUrl);
}
Enable javascript
Add your own javascript
Register your own webviewClient, overriding the onPageFinished to insert a bit of javascript
In the javascript, acquire the element.innerText of the tag, and pass it to your javascript interface.

How can I open my links in one webview in android?

I have a ListView of some websites. What I want is that when a user clicks on listview, those websites are shown in same webview instead of starting a new activity. How can I do that?
Here is my webview java:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Aftonbladet extends Activity {
private WebView myWebView;
/** Called when the activity is first created. */
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { // Enables browsing to previous pages with the hardware back button
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",
true);
myWebView = (WebView) findViewById(R.id.mywebview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(pd.isShowing() && pd!=null)
{
pd.dismiss();
}
}
});
myWebView.loadUrl("http://www.google.com");
}
}
For website loading in webview, you can use like this:
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.linkedin.com");
webview.getSettings().setPluginState(PluginState.ON);
webview.getSettings().setAllowFileAccess(true);
webview.setWebViewClient(new MyWebViewClient());
and your MyWebViewClient class is:
private class MyWebViewClient extends WebViewClient {
#Override
//show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl (url);
return true;
}
}
Hope it helps
EDIT: It looks like what you want is to pass the url from one activity to another. To achieve that you can use either SharedPreferences or putExtras to the intent like this in your ListView Activity:
Intent myIntent = new Intent(this, Aftonbladet.class);
Bundle b = new Bundle();
b.putString("url", theURL);
myIntent.putExtra(b);
startActivity(myIntent);
and then on your WebView Activity you can get that value:
Bundle b = this.getIntent().getExtras();
String url = b.getString("url");

Jsoup HTML Form Webview

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.

Categories

Resources