Sohaibm96
Sohaibm96

Reputation: 31

link html button to java activity

I'm making an Android app to my book I have an html page I want to add a button on it which is linked to this Java code

public class MainActivity extends Activity {

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

    // listeners of our two buttons
    View.OnClickListener handler = new View.OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.buttonShareTextUrl:
                    shareTextUrl();
                    break;


            }
        }
    };

    // our buttons
    findViewById(R.id.buttonShareTextUrl).setOnClickListener(handler);


}

// Method to share either text or URL.
private void shareTextUrl() {
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("text/plain");
    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

    // Add data to the intent, the receiving app will decide
    // what to do with it.
    share.putExtra(Intent.EXTRA_SUBJECT, "book");
    share.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=com.sohaibm.assrar_bac");

    startActivity(Intent.createChooser(share, "share with"));
}

}

so I want you to give me an HTML code of the page that contains the button linked to this activity and thank you so much

Upvotes: 2

Views: 2017

Answers (2)

Valentun
Valentun

Reputation: 1711

If you use WebView to show book content you can use WebViewClient:

WebViewClient mWebClient = new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("handle:")){
                // do something
                return true;
            }

            else{
                view.loadUrl(url);
            }
            return true;
        }
    };
    mWebView.setWebViewClient(mWebClient);

And your link should be:

<a href="handle://it.to.me">Link text</a>

Upvotes: 2

Chandra Kumar
Chandra Kumar

Reputation: 4205

Try like this:

Html Page:

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="style.css" />

        <script type="text/javascript">

        function moveToScreenTwo() {
        Android.moveToNextScreen();
        }
        </script>

    </head>

  <body>
     <div>
            <input type="button" value="Locate" onClick="moveToScreenTwo()" />
        </div>
  </body>
  </html>

FirstACtivity.java:

import android.webkit.JavascriptInterface;

webView = (WebView) findViewById(R.id.load_url);
webView.getSettings().setJavaScriptEnabled(true);

 if (new File(url).exists()) {
            webView.loadUrl(FILENAME_PREFIX + url);
            Log.d("fileurl", "" + FILENAME_PREFIX + url);
  webView.addJavascriptInterface(new WebAppInterface(this), "Android");
        }
  //Class to be injected in Web page
    public class WebAppInterface {
        Context mContext;

        /**
         * Instantiate the interface and set the context
         */
        WebAppInterface(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void moveToNextScreen() {

            Intent i = new Intent(FirstActivity.this,SecondActivity.class);
            startActivity(i);

        }
     }

For more Reference: Check this tutorial.

Upvotes: 0

Related Questions