Click here to Skip to main content
15,886,110 members
Articles / Web Development
Tip/Trick

Xamarin: Starting an Activity from Webview

Rate me:
Please Sign up or sign in to vote.
4.40/5 (3 votes)
26 Feb 2018CPOL1 min read 11.5K   1  
Open a Phone Dialer on click of a link in Webview

Introduction

Being a web developer entices me knowing how I can use my web skills in Xamarin as well. With the quest to do this, I found Webview. So in short, it is a browser which can display HTML within your App. Well, it can also run your JavaScript and style your HTML using CSS.

Background

What I had to achieve here was to fire up Phone dialer when I click on the link which contains tel:. This is something similar to mailto: which will trigger your email client to open up and you can write email to the address specified.

Using the Code

Well just adding the link doesn't work in the Webview. So we need to create an Intent to start the Phone dialer. The solution below has been implemented in Android for now and not on iOS. Will put an addendum once done.

So to do this, we use Dependency Injection like this:

  1. Create an Interface in your PCL:
    C#
    public interface IAppIntents
    {
        void HandleWebviewUri(string uri);
    }
    
  2. Now create a class in your Android project to implement the above interface:
    C#
    public class AppIntents_Droid : IAppIntents
    {
        public void HandleWebviewUri(string uri)
        {
            var appUri = Android.Net.Uri.Parse(uri);
            var appIntent = new Intent(Intent.ActionView, appUri);
            Application.Context.StartActivity(appIntent);
        }
    }
    
  3. Whenever user clicks on a link in Webview, a Navigating event is fired which we need to capture and call the above method:
    C#
    void WvMain_Navigating(object sender, WebNavigatingEventArgs e)
    {
        If(e.Url.Contains("tel:")
        {
            DependencyService.Get<iappintents>().HandleWebviewUri("tel:+9809801234");
        }
    }
    

Pretty simple right!

Points of Interest

Xamarin is a wonderful framework to work with if you have a craving to develop Web apps. With Webview, you can extend your existing Web developer skills to develop great mobile apps. Do let me know if you implement it in your project and if you find this article helpful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
I am a senior software developer. I work for a web development company as team lead. Most of the solutions developed by myself are for web using DotNet Framework but do have keen interest in developing for mobile app as well. At work I am responsible to provide solutions to clients and work with dynamic teams developing for web and mobile. I have been closely working with teams and mentoring them developing in technologies like ASP.Net and PHP for Web and Objective C, Android SDK and cross platform Mobile app.

I like to try my hands-on various other web and mobile development technologies. My current interests apart from ASP.Net (WebForms, MVC and DotNet Core) is for NodeJS, Angular, React and Typescript for developing Web and Mobile Apps. I have been constantly exploring new technologies and implementations for CI/CD, Source Control & Versioning and Automation. Also, looking forward to work on Bots, Machine learning, IoT, VR & AR.

I use tools to do my work and suit my workflows. I was made aware of blogging during my training at Aptech and has been constantly working on having blogs for myself and my work. Including this blog there are few more links of my online presence mentioned below.

Comments and Discussions

 
-- There are no messages in this forum --