Draw 2D Graphics 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.

2D Graphics is called to draw the oval, circle, and line in Xamarin Android app.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, mentioned below, are required to be followed in order to draw 2D graphics in 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).

Templates

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.

Templates

Step 3

In this step, delete the Main.axml file. In this app, you do not require Main.axml file.

Now, Go to Solution Explorer-->Resource-->Layout-->Main.axml-->Right click-->Delete.

Step 4

In this step, add one Class file and it's name is MyOvealShape.cs.

Go to Solution Explorer-->Resource-->Right click-->Add-->New Item (Ctrl+shift+A).

Templates

Step 5

Now, select the class file, give the name MyOvelShape.cs and click add.

Templates

Step 6

In this step, add Namespace in MyOvelShape.cs class file, as mentioned below.

MyOvalShape.cs

  1. using Android.Graphics;  
  2. using Android.Graphics.Drawables;  
  3. using Android.Graphics.Drawables.Shapes;  
Templates

Step 7

Now, write the code, shown below from MyOvalShape.cs page.

Add one sub class, where name is called View
  1. public class MyOvalShape: View {  
  2.         private readonly ShapeDrawable _shape;  
  3.         public MyOvalShape(Context context): base(context) {  
  4.             var paint = new Paint();  
  5.             paint.SetARGB(255, 200, 255, 0);  
  6.             paint.SetStyle(Paint.Style.Stroke);  
  7.             paint.StrokeWidth = 4;  
  8.             _shape = new ShapeDrawable(new OvalShape());  
  9.             _shape.Paint.Set(paint);  
  10.             _shape.SetBounds(20, 20, 300, 200);  
  11.         }  
  12.         protected override void OnDraw(Canvas canvas) {  
  13.             _shape.Draw(canvas);  
  14.         }  
Templates

Step 8

In this step, go to the MainActivity.cs page, add one variable and write the code, shown below from OnCreate() method.

MainActivity.cs
  1. public class MainActivity: Activity {  
  2.     int count = 1;  
  3.     protected override void OnCreate(Bundle bundle) {  
  4.         base.OnCreate(bundle);  
  5.         SetContentView(new MyOvalShape(this));  
  6.     }  
  7. }  
Templates

Step 9

If you have an 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 the Run option.

Templates

Output

After a few seconds, the app will start running on your phone.

You will see the 2D circle drawing is successful.



Summary

This was the process to draw 2D Graphics in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all