19

I have this html:

<p>
    <a href="http://en.wikipedia.org/wiki/Sauropelta">sawr-o-pel-te</a> meaning 'lizard shield'
</p>
<p>
    April 7th, 2015
</p>
<p>
    4/7/2015
</p>
<p>
    April 7th
</p>
<p>
    Next Monday 6th<br>
</p>
<p>
    202 E South St<br>
    Orlando, FL 32801
</p>
<h3>Quick Facts</h3>
<ul>
    <li>One of the most well-understood nodosaurids<span></span></li>
    <li>The earliest known genus of nodosaurid<span></span></li>
    <li>Measured about 5 meters (16.5 ft) long<span></span></li>
    <li>The tail made up nearly half of its body length</li>
</ul>
<span></span>

And I want to know if its possible to automatically hyperlink the dates so when the user presses on them you can add them to the users(phone's) calendar? A good example of how this should work is Gmail. When there is a date or the word(tomorrow, this friday, etc..) it should be automatically linked so the date can be added to calendar.

Update:

Does any one know if there is a ex. javascript that I can add to the app that will do this job for me?

Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
  • Is the html on your question inside an android app ? – Pedro Lobito Apr 30 '15 at 13:38
  • @PedroLobito thanks for your answer, but this html is comming from the server side and I cannot add the `#addtocalendar` tag. So do you have any other workaround on your mind? – Darko Petkovski Apr 30 '15 at 21:20
  • yes, you can [download the html source from the website](http://stackoverflow.com/a/4571551/797495), convert every link with a regex, or similar tool, and load the transformed html to webView with `loadData`, i.e. `webView.loadData(stringWithHtml, "text/html; charset=UTF-8", null);` – Pedro Lobito May 01 '15 at 02:04

2 Answers2

5

Yes, it is possible.

Use the WebViewClient method shouldOverrideUrlLoading to intercept any links containing a certain value. Let's say you create a link like this:

<a href="#addtocalendar"> April 7th, 2015 - Add to Calendar</a>

Now, we'll use the shouldOverrideUrlLoading to intercept the tap and add the event to calendar, i.e.:

    mWebView.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView wView, String url)
        {
            if (url.contains("addtocalendar") ) {
               Calendar cal = Calendar.getInstance();              
               Intent intent = new Intent(Intent.ACTION_EDIT);
               intent.setType("vnd.android.cursor.item/event");
               intent.putExtra("allDay", false);
               intent.putExtra("rrule", "FREQ=YEARLY"); //optional recurring event
               intent.putExtra("beginTime", cal.getTimeInMillis());
               intent.putExtra("endTime", cal.getTimeInMillis()+3600000); // adds 1 hour
               intent.putExtra("title", "Event on April 7th, 2015");
               startActivity(intent);

               return true;
           }
      }
});

Add the following permissions to AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

shouldOverrideUrlLoading

boolean     shouldOverrideUrlLoading(WebView view, String url)

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView.

http://developer.android.com/reference/android/webkit/WebViewClient.html


Android Calendar Provider

http://developer.android.com/guide/topics/providers/calendar-provider.html

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

You can use this link to directly jump to particular date.

Where, date = YYYYMMDD and mode = day/month according to your need.

Hope this helps.

itzmebibin
  • 9,199
  • 8
  • 48
  • 62
Harsh Dattani
  • 2,109
  • 1
  • 17
  • 27