1

Wondering how to make webview go back.

Tried putting in the webview back on press but it seems that with my code it's not working. Not sure if i initiated it, still confused about this.

Also is it possible to get rid of scrollbars in webview? i found some posts about it but am still on the fence about how exactly to put it into my code

package com.webapp.area956;

  import android.os.Bundle;
  import android.app.Activity;
  import android.view.KeyEvent;
  import android.view.Menu;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;

public class MainActivity extends Activity {





@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebViewClient yourWebClient = new WebViewClient()

        {
           // Override page so it's load on my view only
           @Override
           public boolean shouldOverrideUrlLoading(WebView  view, String  url)
           {
            // This line we let me load only pages inside Firstdroid Webpage
            if ( url.contains("area956") == true )
               // Load new URL Don't override URL Link
               return false;

            // Return true to override url loading (In this case do nothing).
            return true;
           }
       };

    String url = "http://www.area956.com";
    WebView view = (WebView) this.findViewById(R.id.webView1);
    view.setWebViewClient(yourWebClient);
    view.getSettings().setJavaScriptEnabled(true);
    view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    view.loadUrl(url);

}
public void onBackPressed (){

    if (WebView.canGoBack()) {
            WebView.goBack();       
    }
    else {
            super.onBackPressed();
            finish();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1 Answers1

0

Have webview as a class member

WebView view;
@Override
public void onCreate(Bundle savedInstanceState) {

Then initialize

view = (WebView) this.findViewById(R.id.webView1);
view.setWebViewClient(yourWebClient);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
view.loadUrl(url);

Then use onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
        view.goBack();
        return true;
    }
    else
    {
          finish();
          return true;
    }
}

You have declared WebView view in onCreate and initialized it there. So it becomes local to onCreate.

Edit:

You can also use onBackPressed as of 2.0

http://android-developers.blogspot.in/2009/12/back-and-other-hard-keys-three-stories.html

  public void onBackPressed (){
    super.onBackPressed();
    if (view.canGoBack()) {
            view.goBack();       
    }
    else {

            finish();
    }
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • AWESOME! HELL YEAH! Thanks so much, mind if i ask another question in regards to allowing external sites to open in the browser. Would it be the opposite of what i have for shouldoverrideurlloading? – user2923771 Oct 27 '13 at 07:55
  • you can use intent and provide the url and open the link in browser. – Raghunandan Oct 27 '13 at 08:09
  • Intent? i know it's a lot to ask but would you mind explaining that as well? – user2923771 Oct 27 '13 at 08:11
  • @user2923771 http://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application. check the answer – Raghunandan Oct 27 '13 at 08:12
  • Intent goes after the Webview class? I placed it there and am getting an error. `WebView view; Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); startActivity(browserIntent); ` – user2923771 Oct 27 '13 at 08:27
  • place it in activity class inside button click it opens in browser and not webview – Raghunandan Oct 27 '13 at 08:27
  • Okay got it! thanks again, would i create a different intent for each site i want to open in the browser? – user2923771 Oct 27 '13 at 08:30
  • @user2923771 left to you how your design should be – Raghunandan Oct 27 '13 at 08:32
  • thanks so much for all your help! I greatly appreciate your insight and completely hope you have an awesome day! – user2923771 Oct 27 '13 at 08:35