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).
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.
Step 3
Next, go to the Solution Explorer. Go to Resource --> Layout --> double click to open Main.axml page.
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.
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.
Step 6 In this step, open the String.xml page. Go to the Solution Explorer --> Resource --> Values --> String.xml.
Step 7 After opening the String.xml file, write the following XML code.
String.xml - <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="Hello">Hello World, Click Me!</string>
- <string name="ApplicationName">Auto_Link</string>
- <string name="website">www.xamarin.com</string>
- </resources>
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").
Step 9 Also, edit the autoLink value (Ex: android:autoLink="web" ).
Step 10 In this step, go to the Main.axml page Source Panel and note down the TextView id value.
Main.axml - <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">
- <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>
Step 11 Go to the MainActivity.cs page in Solution Explorer. Write the following code in OnCreate() Method.
MainActivity.cs - protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
- }
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.
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.
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.