I have a web application that works well but I cannot get the back button to function in the web view. Please see code:
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
public class extends Activity
{
final Activity activity = this;
WebView WebView;
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.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setUseWideViewPort(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://www.example.com");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (WebView != null && (keyCode == KeyEvent.KEYCODE_BACK) && WebView.canGoBack()) {
WebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
The application runs but when the back button is pressed it still exits the application. I am also wanting to add the ability to use a html mailto tag with the application. Any help would be appreciated. Thank you.
Edit:
Include a null check to see if WebView is null before invoking methods on it.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (WebView != null && (keyCode == KeyEvent.KEYCODE_BACK) && WebView.canGoBack()) {
WebView.goBack();
return true;
}
Your code seems correct to me. What is the exact behavior you are hoping to achieve from the back button?
What currently happens when you run the application and press the back button?
Edit, Oh I see what is happening.
You are trying to override onKeyDown() inside your onCreate(). onKeyDown() and onCreate() are both methods of Activity, thus you cannot declare one inside the other.
Try your code like this:
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
public class DealJargon extends Activity {
final Activity activity = this;
WebView WebView;
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.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("http://www.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) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && WebView.canGoBack()) {
WebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Edit 2: Anytime you get some kind of compile error that has something to do with a brace it is likely that you are trying to declare a method inside the declaration of another method. Or something similar.
Related
I'm facing 'Unexpected token' error in if-else condition using &&. There's no any other error can be seen.
keyCode==KeyEvent.KEYCODE_BACK) && webViewsupport.canGoBack
I'm not able to understand that why it's showing Unexpected token here for using &&.
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class Support extends AppCompatActivity {
WebView webViewsupport;
ProgressBar progressBar;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_support);
webViewsupport = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar)findViewById(R.id.progressBar2);
webViewsupport.setWebViewClient(new myWebClient());
webViewsupport.getSettings().setJavaScriptEnabled(true);
webViewsupport.loadUrl("https://example.com");
}
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK) && webViewsupport.canGoBack(){
webViewsupport.goBack();
}
return super.onKeyDown(keyCode, event);
}
}
This is screenshot
There's a typo in your code (your ending parentheses is placed at the wrong spot):
if (keyCode == KeyEvent.KEYCODE_BACK) && webViewsupport.canGoBack(){
webViewsupport.goBack();
}
Should be:
if (keyCode == KeyEvent.KEYCODE_BACK && webViewsupport.canGoBack()) {
webViewsupport.goBack();
}
Voted to close for this reason.
You need to add the whole condition in () :
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK && webViewsupport.canGoBack()){
webViewsupport.goBack();
}
return super.onKeyDown(keyCode, event);
}
Hello i have java code where progressBar Loads just for first time only.
second time when surfing inner pages it does not shows at all.
package ext.packagename.apk;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
WebView web;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
OneSignal.startInit(this).init();
web = (WebView) findViewById(R.id.activity_main_webview);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//open in webview
//web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://example1.com");
// opening in browser instead of WebView
web.setWebViewClient(new MyAppWebViewClient());
}
public class MyAppWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("exmaple1.com") || Uri.parse(url).getHost().endsWith("example2.com") || Uri.parse(url).getHost().endsWith("example3.com") ) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
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);
progressBar.setVisibility(View.GONE);
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
} else {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Alert")
.setMessage("exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
return super.onKeyDown(keyCode, event);
}
}
Progressbar is loading very fine at first time when main page loads. after that it disappear.
Use
progressBar.setVisibility(View.VISIBLE);
in onPageStarted method
I have my WebViewFragment and of course i want to go back in the site im browsing but i have the error:
The method onBackPressed() is undefined for the type Fragment
Why? I don´t understand that?
What am i doing wrong?
Here´s my full webview code i´m using.
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewFragment extends Fragment {
WebView webView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Retrieving the currently selected item number
int position = getArguments().getInt("position");
String url = getArguments().getString("url");
// List of rivers
String[] menus = getResources().getStringArray(R.array.menus);
// Creating view corresponding to the fragment
View v = inflater.inflate(R.layout.fragment_layout, container, false);
// Updating the action bar title
getActivity().getActionBar().setTitle(menus[position]);
//Initializing and loading url in webview
webView = (WebView)v.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl(url);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
return v;
}
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}}
If i´m using:
public void onBackPressed() {
Fragment webview = getFragmentManager().findFragmentByTag("webview");
if (webview instanceof WebViewFragment) {
boolean goback = ((WebViewFragment)webview).canGoBack();
if (!goback)
super.onBackPressed();
}
}}
I have the errors:
The method canGoBack() is undefined for the type WebViewFragment
The method onBackPressed() is undefined for the type Fragment
Thank you dudes for your help!
Found the solution what is working for me in my fragment.
webView.setOnKeyListener(new 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();
return true;
}
break;
}
}
return false;
}
});
return v;
I am beginner in android development. Below i give my main activity.java code. My requirement if internet connection or wifi not working on mobile devices then following thing perform
If internet connection not working then show the my splash screen backrgound on start of application and give the dialogue box or alert like this screenshot of trip advisor Check this image and screenshot. If cancel button press then dialogue box disappear and only display splash screen and if press try again then try to perform connect to internet.
In my code i take the code buy dialogue box, i not want buy function this is not my requirement. i want exactly like trip advisor. For me easy if somebody edit the my code and show where make changes very thanks in advance
package com.example.edarabia;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity{
WebView mywebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = (WebView) findViewById(R.id.webview);
if(isNetworkConnected() == true){
mywebview.getSettings().setJavaScriptEnabled(true);
mywebview.setWebViewClient(new myWebClient());
mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");
mywebview.getSettings().setBuiltInZoomControls(true);
mywebview.getSettings().setLoadWithOverviewMode(false);
mywebview.getSettings().setUseWideViewPort(false);
}else{
showBuyDialog();
}
}
// #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) && mywebview.canGoBack()) {
// mywebview.goBack();
// return true;
// }
// return super.onKeyDown(keyCode, event);
// }
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
mywebview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
finish();
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
//// This method will retun boolean value if net conect then value will be true otherwise false
private boolean isNetworkConnected() {
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
public void showBuyDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("just for testing");
builder.setMessage("Check you net conectivity....");
builder.setCancelable(false);
builder.setPositiveButton("Buy", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent browserIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://www.google.com"));
startActivity(browserIntent);
}
});
builder.show();
}
}
This might be helpful:
Detect whether there is an Internet connection available on Android
or try digging into BroadcastReceivers.
Sample code:
public class InternetConnectionStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
_v("Network connectivity change");
if (intent.getExtras() != null) {
NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
_v("Network " + ni.getTypeName() + " connected");
onNetworkConnection(context, true);
}
}
if (intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
_v("There's no network connectivity");
onNetworkConnection(context, false);
}
}
private void onNetworkConnection(Context context, boolean isConnected) {
//show dialog
}
}
I got the solution and below the updated code
package com.example.edarabia;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity{
WebView mywebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(isNetworkConnected() == true){
setContentView(R.layout.activity_main);
mywebview = (WebView) findViewById(R.id.webview);
mywebview.getSettings().setJavaScriptEnabled(true);
mywebview.setWebViewClient(new myWebClient());
mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");
mywebview.getSettings().setBuiltInZoomControls(true);
mywebview.getSettings().setLoadWithOverviewMode(false);
mywebview.getSettings().setUseWideViewPort(false);
}else{
setContentView(R.layout.splash);
showBuyDialog();
}
}
// #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) && mywebview.canGoBack()) {
// mywebview.goBack();
// return true;
// }
// return super.onKeyDown(keyCode, event);
// }
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
mywebview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
finish();
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
//// This method will retun boolean value if net conect then value will be true otherwise false
private boolean isNetworkConnected() {
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
public void showBuyDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Unable to connect");
builder.setMessage("You Must have an Internet connection to use Edarabia. Please connect and try again.");
builder.setCancelable(false);
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
builder.show();
}
}
Try to replace showButDialog() method,
public void showBuyDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Unable to connect");
builder.setMessage("You must have an Internet connection to use TripAdvisor. Please connect and try again.");
builder.setCancelable(false);
builder.setPositiveButton("Try again", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
isNetworkConnected();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
And to get the UI same as in the picture, add following line before application tag, like this,
<uses-sdk android:targetSdkVersion="15" />
This kind of dialog is available from Android OS 3.0 (Honey Comb) and above. So you cannot see dialog in previous versions.
I have an application with a WebView.
But I want to make it save URLs, for example: when the application starts, it opens google.com. So if I want to type something in and save that search or website, I want it to have a button: "Save site" and "Load site". How do I do this as simple as possible?
I am using this code so far:
package com.mysoftwaremobileapps.Finnnofortablets;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class finnnoActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button TilbakeButton;
WebView FinnWeb;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FinnWeb = (WebView)findViewById(R.id.FinnWeb);
FinnWeb.setWebViewClient(new MyWebViewClient());
FinnWeb.getSettings().setBuiltInZoomControls(true);
FinnWeb.getSettings().setJavaScriptEnabled(true);
FinnWeb.loadUrl("http://m.finn.no");
FinnWeb.requestFocus(View.FOCUS_DOWN);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
setContentView(R.layout.main);
FinnWeb = (WebView) findViewById(R.id.FinnWeb);
FinnWeb.setWebViewClient(new MyWebViewClient());
FinnWeb.getSettings().setJavaScriptEnabled(true);
FinnWeb.getSettings().setBuiltInZoomControls(true);
FinnWeb.requestFocus(View.FOCUS_DOWN);
FinnWeb.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu meny) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menubuttons, meny);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.LoggInn:
setContentView(R.layout.main);
FinnWeb = (WebView) findViewById(R.id.FinnWeb);
FinnWeb.setWebViewClient(new MyWebViewClient());
FinnWeb.getSettings().setJavaScriptEnabled(true);
FinnWeb.loadUrl("http://m.finn.no/#loginForm.html");
FinnWeb.requestFocus(View.FOCUS_DOWN);
break;
case R.id.OmUtvikler:
setContentView(R.layout.omutvikler);
TilbakeButton = (Button)findViewById(R.id.TilbakeButton);
TilbakeButton.setOnClickListener(this);
break;
case R.id.RegistrerDeg:
setContentView(R.layout.main);
FinnWeb = (WebView) findViewById(R.id.FinnWeb);
FinnWeb.setWebViewClient(new MyWebViewClient());
FinnWeb.getSettings().setJavaScriptEnabled(true);
FinnWeb.loadUrl("http://m.finn.no/#auth/registrer.html");
FinnWeb.requestFocus(View.FOCUS_DOWN);
break;
case R.id.Tilbake:
if (FinnWeb.canGoBack()) {
FinnWeb.goBack();
}
else{
}
break;
case R.id.Frem:
if (FinnWeb.canGoForward()) {
FinnWeb.goForward();
}
else{
}
break;
case R.id.ExitWithSaving:
System.exit(0);
break;
case R.id.Exit:
System.exit(0);
break;
case R.id.LastAnnonse:
break;
}
return true;
}
public void onClick(View src) {
switch(src.getId()) {
case R.id.TilbakeButton:
setContentView(R.layout.main);
FinnWeb = (WebView) findViewById(R.id.FinnWeb);
FinnWeb.setWebViewClient(new MyWebViewClient());
FinnWeb.getSettings().setJavaScriptEnabled(true);
FinnWeb.getSettings().setBuiltInZoomControls(true);
FinnWeb.requestFocus(View.FOCUS_DOWN);
break;
}
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
There are a couple of different ways to do this. The first is to remember the URL via a hook in your already overridden "shouldOverrideUrlLoading".
Like:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{ view.loadUrl(url);
//save off url in instance variable
this.currentURL = url.toString();
return true;
}
Secondly is to call out to your webView to get the current URL when the user clicks on your mentioned "Save Site" button via the method:
http://developer.android.com/reference/android/webkit/WebView.html#getUrl()
Either of these methods will allow you to access what URL is currently being displayed. I hope this helps.