3

Please Refer this example http://www.mkyong.com/android/android-webview-example/

In the above link example when we clink on button it redirects to google home page. Google page opening in webview. On the top i want to put close button. So when i click close button it has to go to previous back page. Please see below image for example.

A screen shot

public class MainActivity extends Activity {
    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar bar = getActionBar();
        bar.hide();
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
                viewx.loadUrl(urlx);
                return false;
            }
        });

    }
}
Satyam
  • 41
  • 2
  • 5
  • Please click on link for image. – Satyam Oct 11 '15 at 00:57
  • You have tagged this as inappbrowser (which is usually taken to mean the customized version of the webview that's found in Cordova) and also as Webview. Which one are you using? Please update your question with that information and also remove one or the other tags. – e4c5 Oct 11 '15 at 03:33
  • You need to create a header on top of your webview. You can use a simple linearlayout with a button in it for this purpose. – Rahul Tiwari Oct 11 '15 at 03:46
  • I have modified my post. Please check it once and help me. Thank you. – Satyam Oct 11 '15 at 19:37
  • Sorry I am new to this site. Could you please explain clearly. – Satyam Oct 11 '15 at 19:45
  • Please help me... @Rahul Tiwari – Satyam Oct 13 '15 at 00:57

1 Answers1

0

Remove these lines from your code:

 ActionBar bar = getActionBar();
 bar.hide();

Now replace your home button on action bar with an 'X' icon and override its action like this:

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case android.R.id.home:
          finish();
    }
    return (super.onOptionsItemSelected(menuItem));
}
Community
  • 1
  • 1
Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78