How To Capture User Input Text In Xamarin Android App Using Visual Studio 2015

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. In Xamarin Studio, Visual Studio is also available.
Use EditText control to capture the text entered by a user.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, given below are required to be followed in order to Capture User Input Text in the Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or click (Ctrl+Shift+N).



Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.



Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and source in your project. Select Resource-->Layout-->double click to open main.axml page. To write XAML code, you need to select the source.

Choose the Designer Window, if you want design it, and you can design your app.



Step 4

After opening main.axml, file will open the main page designer. You can design the page, as per your desire.



Now,  delete the Default hello world button. Go to the source panel and you can see the button coding. You need to delete it.
After deleting XAML code, delete C# button action code. Go to MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls. You need to go to the toolbox Window. Now, scroll down and you will see all the tools and controls. You need to drag and drop the Plain Text (EditText).



Step 6

You need to drag and drop the TextView.



Step 7

Now, go to the properties Window. You need to edit the Plain Text (EditText) Id Value(EX: android:id="@+id/editText").



Step 8

Edit the TextView's Id value.(Ex: android:id="@+id/textView").



Step 9

In this step, go to Main.axml page source panel. Note, the EditText Id's value and TextView's Id value.

Main.axml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.    android:orientation="vertical"  
  3.    android:layout_width="match_parent"  
  4.    android:layout_height="match_parent"  
  5.    android:minWidth="25px"  
  6.    android:minHeight="25px">  
  7. <EditText  
  8.    android:id="@+id/editText"  
  9.    android:layout_width="fill_parent"  
  10.    android:layout_height="wrap_content" />  
  11. <TextView  
  12.    android:id="@+id/textView"  
  13.    android:layout_width="fill_parent"  
  14.    android:layout_height="wrap_content" />  
  15. </LinearLayout>  


Step 10

In this step, go to MainActivity.cs page in Solution Explorer. Write the code, mentioned below Between OnCreate() Method.

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle)  
  2. {  
  3.     base.OnCreate(bundle);  
  4.     // Set our view from the "main" layout resource  
  5.     SetContentView(Resource.Layout.Main);  
  6.     var editText = FindViewById < EditText > (Resource.Id.editText);  
  7.     var textView = FindViewById < TextView > (Resource.Id.textView);  
  8.     editText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => {  
  9.         textView.Text = e.Text.ToString();  
  10.     };  
  11. }  


Step 11

If you have Android Virtual device, run the app on it, else connect your Android phone and run the app on it. 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 Run option.



Output

After a few seconds, the app will start running on your phone. You will see your app runs successfully. Now, you can give the input and see it will work successfully..





Summary

This was the process of how to capture the user input text in Xamarin Android app, using Visual Studio 2015.

 

Up Next
    Ebook Download
    View all
    Learn
    View all