Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you autolink dates in android webview

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?

like image 676
Darko Petkovski Avatar asked Apr 17 '15 04:04

Darko Petkovski


People also ask

Is WebView deprecated in Android?

This interface was deprecated in API level 12. This interface is now obsolete.

What is alternative of WebView in android?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

What is a WebView link?

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application.


1 Answers

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

like image 116
Pedro Lobito Avatar answered Oct 02 '22 13:10

Pedro Lobito