1

I have an Android program to display an HTML page in a web view. The HTML page exists locally in "asset/www/index.html".

I want to put a button on the HTML page and open a new activity when the button is clicked.

Here is the Java code:

public class HelloWebApp extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/www/index.html");
    }
}

1 Answer 1

1

You have to pass url on button click from html like below,

index.html

<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<script type="text/javascript" charset="utf-8" src="jquery-2.0.0.min.js"></script>
<script type="text/javascript" charset="utf-8" src="quantize.js"></script>

<title>My HTML</title>
</head>
<body>
<h1>My HTML</h1>
<INPUT TYPE="button" value="Test" onClick="window.location='Navigation://OpenNativeScreen'">
</body>
</html>

Now you will get the that url in shouldOverrideUrlLoading method of web view when button click.see the below code,

MainActivity.java

public class MainActivity extends Activity {

WebView myBrowser;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dropdown_html);
    myBrowser = (WebView) findViewById(R.id.mybrowser);
    myBrowser.setWebViewClient(new MyBrowser());
    myBrowser.getSettings().setJavaScriptEnabled(true);
    myBrowser.loadUrl("file:///android_asset/www/index.html");
}

private class MyBrowser extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.equals("Navigation://OpenNativeScreen")) {
            startActivity(new Intent(MainActivity.this,SecondActivity.class));
            finish();
            return true;
        }
        return false;
    }       
   }
 }
Sign up to request clarification or add additional context in comments.

8 Comments

LogCat: Error opening trace file: No such file or directory. In Emulator: Webpage not available...
where you have put your html file?
put your file in asstes folder and write the url file:///android_asset/index.html instead of file:///android_asset/www/index.html
Did that...still same result. "Webpage not available". Its not exiting from the webview. It should exit webview and open a new activity.
have you remove WWW from the url ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.