Creating An AutoLink Using TextView In Xamarin Android App

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. The Xamarin Studio is available in Visual Studio too.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, given below, are required to be followed in order to create an autoLink using TextView in a Xamarin Android app.

Step 1

Click File--> select New--> select Project. Or, click (Ctrl+Shift+N).

Xamarin

Step 2
 
After opening the New Project, go to Installed >> Templates >> Visual C# >> Android >> Blank App (Android).
 
Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Xamarin

Step 3
 
Next, go to the Solution Explorer. Go to Resource --> Layout --> double click to open Main.axml page. 

Xamarin

Step 4

The Main.axml file will open the main page designer. In this page, you can select the mode (Designer or Source) as per your choice. 

Xamarin

Next, delete the default "Hello World" button. For this, go to the source panel and delete the button code from there. Similarly, delete the C# button code from the MainActivity.cs page.

Step 5

Now, go to the Toolbox window. Now, scroll down to all the tools and controls. Drag and drop the TextView.

Xamarin

Step 6

In this step, open the String.xml page. Go to the Solution Explorer --> Resource --> Values --> String.xml.

Xamarin

Step 7

After opening the String.xml file, write the following XML code.

String.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="Hello">Hello World, Click Me!</string>  
  4.     <string name="ApplicationName">Auto_Link</string>  
  5.     <string name="website">www.xamarin.com</string>  
  6. </resources>  
Xamarin

Step 8

Now, go to the Properties window. You need to edit the TextView Id Value Text Value (EX  android:id="@+id/textView1" android:text="@string/website").

Xamarin

Step 9

Also, edit the autoLink value (Ex: android:autoLink="web" ).

Xamarin

Step 10

In this step, go to the Main.axml page Source Panel and note down the TextView id value.

Main.axml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px">  
  2.     <TextView android:text="@string/website" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView1" android:autoLink="web" /> </LinearLayout>  
Xamarin

Step 11

Go to the MainActivity.cs page in Solution Explorer. Write the following code in OnCreate() Method.

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     SetContentView(Resource.Layout.Main);  
  4. }  
Xamarin

Step 12

If you have Android Virtual device, run the app on it. Else, connect your Android phone and run the app in that. Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex LENOVO A6020a40(Android 5.1-API 22)).
 
Click the Run option.

Xamarin

Output

After a few seconds, the app will start running on your phone. You will see that your app is running successfully.
 
Just click the Xamarin link.

Xamarin

Now, choose the browser.



The Xamarin website is working successfully.


Summary

So, this was the process of how to create an autoLink using TextView in Xamarin Android app, using Visual Studio 2015.


Similar Articles