Draw Polygon Graphics In Xamarin Android App Using Visual Studio 2015


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.


  • Visual Studio 2015 Update 3.

The steps, given below, are required to be followed in order to Draw Polygon Graphics 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

Next, go to the Solution Explorer and select Resource-->Layout-->double click to open main.axml page. There, you can select either Source mode to write the code or Design mode to design your app.


Step 4

After opening it, the main.axml file will open the main page designer. In this page, you can design this page.


Step 5

In this step, add one Class file called FilledPolygon.cs.

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


Step 6

Now, select the Class file and give name FilledPolygon.cs, and click Add.


Step 7

After creating the FilledPolygon.cs file, add the following Namespace inside the namespace and create one subclass called View.

  1. namespace polygon {  
  2.     using Android.Content;  
  3.     using Android.Graphics;  
  4.     using Android.Views;  
  5.     class FilledPolygon: View {}  
  6. }  

Step 8

Now, write the following code in FilledPolygon.cs page.

  1. class FilledPolygon: View {  
  2.     private readonly PointF[] _points = new [] {  
  3.         new PointF(100, 100),  
  4.             new PointF(200, 200),  
  5.             new PointF(200, 500),  
  6.             new PointF(600, 600),  
  7.             new PointF(400, 200),  
  8.             new PointF(100, 100)  
  9.     };  
  10.     public FilledPolygon(Context context): base(context) {}  
  11.     protected override void OnDraw(Canvas canvas) {  
  12.         base.OnDraw(canvas);  
  13.         var path = new Path();  
  14.         path.MoveTo(_points[0].X, _points[0].Y);  
  15.         for (var i = 1; i < _points.Length; i++) {  
  16.             path.LineTo(_points[i].X, _points[i].Y);  
  17.         }  
  18.         var paint = new Paint {  
  19.             Color = Color.White  
  20.         };  
  21.         paint.SetStyle(Paint.Style.Fill);  
  22.         canvas.DrawPath(path, paint);  
  23.     }  
  24. }  

Step 9

Next go to the MainActivity.cs page from Solution Explorer. write the following namespace inside the namespace. and write the contentview code between the OnCreate() Method.

  1. namespace polygon {  
  2.     using Android.App;  
  3.     using Android.OS;  
  4.     using Android.Widget;  
  5.     [Activity(Label = "polygon", MainLauncher = true, Icon = "@drawable/icon")]  
  6.     public class MainActivity: Activity {  
  7.         protected override void OnCreate(Bundle bundle) {  
  8.             base.OnCreate(bundle);  
  9.             // Set our view from the "main" layout resource  
  10.             SetContentView(Resource.Layout.Main);  
  11.             SetContentView(new FilledPolygon(this));  
  12.         }  
  13.     }  
  14. }  

Step 10

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.



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

You will see the Polygon Graphics is running.



So, this was the process of drawing Polygon Graphics in Xamarin Android app, using Visual Studio 2015.


Recommended Ebook

Xamarin.Forms For Beginners

Download Now!
Similar Articles