Download Code

Xamarin TextView Example Android

In this tutorial you will learn about xamarin TextView example android application.How to set text in a TextView.

Let me give you some introduction about View.

View

View is a Base Class of Widgets.It is basic building block for UI components.It occupies rectangular area on screen.It handle every event on the screen like drawing etc.

TextView

Displays text to the user which can optionally be edited.Basically Text View is complete text editor but basic configuration not allows to edit it.

OK let’s start go ahead and create android project.File > New > Solution.

Text View,TextView,textview in xamarin android,textview xamarin android tutorial,Xamarin TextView,Xamarin TextView Example,TextView Example Android,Xamarin TextView Example Android,

After creating project go to your Resources > Layout directory and open Main.axml.You have two options to add UI component.

  • Designer(used for UI drag and drop component)
  • Source(use xml form)

On the right side you’ll see Tool Box.These are the components which you can drag and drop in Designer Mode.Like this.

Text View,TextView,textview in xamarin android,textview xamarin android tutorial,Xamarin TextView,Xamarin TextView Example,TextView Example Android,Xamarin TextView Example Android,

So,in this tutorial i’ll use Text View and a button to set any text.I’ll just drag and drop a button and text view and give them id’s.Both Mode will look like this.

Text View,TextView,textview in xamarin android,textview xamarin android tutorial,Xamarin TextView,Xamarin TextView Example,TextView Example Android,Xamarin TextView Example Android,

Next i’ll go to MainActivity.cs class and create variables for button and text View.Then refering id’s of both UI components like this.

namespace text view
{
	[Activity (Label = "textview", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		
		Button clickBtn;   // button variable
		TextView displayTxt;   // text_view variable
		// oncreate method
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);
			clickBtn = FindViewById< Button > (Resource.Id.myButton);   // refering button id of axml
			displayTxt = FindViewById< TextView > (Resource.Id.textView1);  // referring text_view id of axml

		}
	}
}

Now apply click event on button to change text of text view. Previously i used delegates but now i’ll use click standalone method to perform click event like this.

/* All Rights Reserved By Farhan Rasheed */
using Android.App;
using Android.Widget;
using Android.OS;

namespace textview
{
	[Activity (Label = "textview", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		
		Button clickBtn;   // button variable
		TextView displayTxt;   // text_view variable
		// oncreate method
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);
			clickBtn = FindViewById< Button > (Resource.Id.myButton);   // refering button id of axml
			displayTxt = FindViewById< TextView > (Resource.Id.textView1);  // referring text_view id of axml
			clickBtn.Click += ClickBtn_Click;  // performing click event
		}

		void ClickBtn_Click (object sender, System.EventArgs e)
		{
			displayTxt.Text = "Android Codec Change Me";
		}
	}
}

Now Run your android project.First it will display like this.

Text View,TextView,textview in xamarin android,textview xamarin android tutorial,Xamarin TextView,Xamarin TextView Example,TextView Example Android,Xamarin TextView Example Android,

When you click on button output will be like this Screen.

Text View,TextView,textview in xamarin android,textview xamarin android tutorial,Xamarin TextView,Xamarin TextView Example,TextView Example Android,Xamarin TextView Example Android,

that’s it for Xamarin TextView Example android app tutorial.I hope you got everything clearly.

Thanks.