0

enter code hereI am new to android development. I am currently working on an app where it uses webview. In that webview I am passing an url and when the url loads that corresponding page has a button inside the webpage that has the functionality to exit the Webpage. My requirement is to get the click function of that button. How is it possible to implement it in android. If anyone knows please help

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />





public class WebviewActivity extends Activity {

WebView web;
String rooId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    Intent intent = getIntent();
    rooId = intent.getStringExtra("RoomId");
    Log.e("Roominer", "Roominer" + rooId);
    web = (WebView) findViewById(R.id.webView1);



    WebSettings settings = web.getSettings();
    settings.setJavaScriptEnabled(true);
    web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    web.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i("", "Processing webview url click...");
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            Log.i("", "Finished loading URL: " + url);

        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
           Toast.LENGTH_SHORT).show();





}

}

Gree
  • 1
  • 1
  • 3
  • Put your xml file please – dpulgarin Mar 22 '16 at 10:59
  • you need to call `setWebViewClient` on your `WebView` – V-rund Puro-hit Mar 22 '16 at 11:06
  • I am using setWebViewClient but i am not sure how call an Onclick in button. web.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.i("", "Processing webview url click..."); view.loadUrl(url); return true; } – Gree Mar 22 '16 at 11:09
  • You stated has the functionality to exit the Webpage. if this is true that that should work for sure. can you post your Java code here? – V-rund Puro-hit Mar 22 '16 at 11:11
  • are you able to display webpage with this code? because you are not loading url outside `setWebViewClint` method. – V-rund Puro-hit Mar 22 '16 at 11:40
  • sorry..my mistake..i forgot to add the line web.loadUrl(url);..yeah it is loading – Gree Mar 22 '16 at 11:41
  • then there must me problem in webpage you created. look if it is working in browser. – V-rund Puro-hit Mar 22 '16 at 11:43
  • This same app is done for IOS. It is working fine – Gree Mar 22 '16 at 11:45
  • 1
    Possible duplicate of [Detect click on HTML button through javascript in Android WebView](http://stackoverflow.com/questions/4065312/detect-click-on-html-button-through-javascript-in-android-webview) – Bö macht Blau Mar 22 '16 at 11:53
  • @Gree - take a look especially at the [answer by peastman](http://stackoverflow.com/a/7380446/5015207) – Bö macht Blau Mar 22 '16 at 11:54
  • just want to make sure..So this problem can only be solved with the help from Web developer – Gree Mar 22 '16 at 12:03

2 Answers2

1

To link your Java code and Javascript code together, you can use something called a JavascriptInterface

This guide will give you a good idea to set this up.

https://developer.android.com/guide/webapps/webview.html

Don't forget to add the @JavascriptInterface annotation above the methods you want to use. This was implemented in SDK 17:

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
-1

You can defined a button in your xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

and set onClickListener at the button:

button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Put the action
            }
        });
dpulgarin
  • 556
  • 5
  • 17