Xamarin Activity,Layout

In this tutorial you will learn how to create xamarin activity.How to start an Activity.Basically you will create a new activity and next you will create new layout for your newly created activity.Next you will start new activity.

Let’s start with some introduction of Layouts and different type of layouts.

Layout

Layout is visual structure for user on which you can use different UI components like button for user interaction.Layout basically created for an activity because when ever you press any button it will show Toast because you have coded that Toast in your Activity.

You can create layout in two ways

  • Declare a UI component in XML
  • Instantiate layout elements at runtime

for more detail you can visit Android Official Website.

you always remember one thing that every UI component in layout including layout have width and height.Like

Xamarin Activity,Xamarin Layout

Different type of Layout

  • Relative Layout

    One of the basic layout to design UI in android.In this layout you can display elements or components with relative position which means where ever you drop your UI component it will show on that particular position whether you drop one component on another component.

This image show you how Relative Layout will look like.

Xamarin Activity,Xamarin Layout

Now you can see i droped UI components on different positions and they are displaying on that particular position.

  • Linear Layout

    In this layout every component or element display in two direction either in Vertical or Horizontal.When ever you drop any UI component it will display in sequence.This image might illustrate something more about this layout.

Vertical Linear Layout

Xamarin Activity,Xamarin Layout

Horizontal Linear Layout

Xamarin Activity,Xamarin Layout

  • Table Layout

    In this layout every UI element or component display in a row or in a column by adding tableRow element in Table Layout.This image will help you to understand what exactly Table Layout is.

Xamarin Activity,Xamarin Layout

  • Frame Layout

This Layout basically can hold only one child view except first UI component every component will display on              top of below UI components you can say overlapping of components just like this.In this Image Toggle Button              and Button are overlapping you can see.

Xamarin Activity,Xamarin Layout

Now let’s start with coding and creating new layout and new activity.

Create new android project.I named it layoutActivity.

whenever you create new android project it’s default layout and Activity will be in android project which might be named as (Main.axml) and (MainActivity.cs).

so what you have to do next just drag and drop one button on your Graphical AXML layout because we will start new activity by clicking that button.Then reference that button id and apply button click event on your button in your (MainActivity) just like this.

namespace layoutactivity
{
	[Activity (Label = "layoutactivity", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		

		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it
			Button button = FindViewById< Button > (Resource.Id.myButton);
			
			button.Click += delegate {
				
			};
		}
	}
}

Activity

Now next you will create new activity by going into your android project.Right click go to Add > New File > Activity and give it a name(abc.cs).

Xamarin Activity,Xamarin Layout

Next we will create a layout for newly created activity.Go to your android project find “Resources” directory and within that directory find “Layout” directory.Right click on “Layout” Add > New File > Layout like.

Xamarin Activity,Xamarin Layout

You don’t need to select type of layout.By default it’s layout is Linear.Now i will drag and drop Text View on second layout.Next use SetContentView() method to set AXML layout like this(abc.cs) and (abc.axml).

Xamarin Activity,Xamarin Layout

Now apply click event on first layout button to start new activity.To start new activity i’ll use startActivity() method with reference object using an expression ‘typeof()’.MainActivity.cs will be.

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

namespace layoutactivity
{
	[Activity (Label = "layoutactivity", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		

		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it
			Button button = FindViewById< Button > (Resource.Id.myButton);
			
			button.Click += delegate {
				
				StartActivity(typeof(abc));
			};
		}
	}
}

Now Run your xamarin activity android project.At the start it will look like.

Xamarin Activity,Xamarin Layout

When you click button it will move to next layout.

Xamarin Activity,Xamarin Layout

that’s it for Xamarin Activity,Layout tutorial.I hope you get the information right.

Thanks.