Create A Flash Light App In Xamarin.Android Using Visual Studio

Introduction and Background

My friend received a Samsung mobile phone few month ago. She liked it yet one thing that it doesn't have is the toolbox for fundamental utilities. One of them is the spotlight application that triggers the gadget to enact or deactivate the electric lamp as required. Android supports you having the Camera API devoured in your application (an electric lamp is given to photographs or recordings and not as a floodlight). Anyway, I was thinking that I ought to get an application from Play Store, correct? No! Despite the fact that it would give me an answer for my spotlight issue, shouldn't something be said about the barrage of ads  I had to experience, or the hazy UI of the application? So I figured I should manufacture an application for her to have the capacity to trigger the spotlight on or off.

Since I made it for her, why not show you all as well, so you can utilize a similar application, or expand it and share it with me so I can likewise utilize your abilities.

In this article I will talk about Xamarin.Android for Camera API. In spite of the fact that this sort of use is exceptionally essential and straight-forward, at any rate it will furnish you with an extremely fundamental way of utilizing Camera API.


You are not required to have a an advanced understanding of Android APIs or how Linux functions, later authorizations and the rest will be discussed in the article. Be that as it may, I will attempt to ensure that I am as clear as an apprentice in this idea would need me to be.

Step 1

Create/ open your Android solution in Visual Studio or Xamarin Studio. Select Android and from list choose Android Blank App.

Step 2

First of all, in References add a reference Xamarin.Android.Support.v7. AppCompat using NuGet Package Manager, as shown below.


Step 3

After Successful Installation of AppCompat, add an XML File in values Folder with name style. In this file add some Code Theme Colors, as shown below.

(Folder Name: values, File Name: style.xml)

XML Code 

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <resources>  
  4.   <style name="MyTheme" parent="MyTheme.Base">  
  6.   </style>  
  8.   <!--Base theme applied no matter what API-->  
  9.   <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">  
  10.     <item name="windowNoTitle">true</item>  
  11.     <!--We will be using the toolbar so no need to show ActionBar-->  
  12.     <item name="windowActionBar">false</item>  
  13.     <!--Set theme color from>  
  14.     <!--Color Primary is used for default action bar backround-->  
  15.     <item name="colorPrimary">#009688</item>  
  16.     <!--Color Primary Dark is use for status bar-->  
  17.     <item name="colorPrimaryDark">#1976D2</item>  
  18.     <!--ColorAccent is used as the default value for   
  19.     ColorControlActivated which is used to tint widgts-->  
  20.     <item name="colorAccent">#FF4081</item>  
  22.     <item name="colorControlHighlight">#FF4081</item>  
  23.     <!--You can also set ColorControlNormal ColorControlActivated   
  24.       ColorControlHihglight colorSwitchThumbNormal.-->  
  25.   </style>  
  27. </resources>  

Step 4

Now, add a new Folder in Resources Folder with name values-v21. In values-v21 Folder Add a new XML file with name style and insert some Code for Theme Properties, as shown below.

(Folder Name: values-v21, File Name: style.xml) 

XML Code

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <resources>  
  3.   <!--  
  4.   Base Application theme for API 21+. this theme   
  5.   replaces MyTheme from res/style.xml on API 21+ devices-->  
  7.   <style name="MyTheme" parent="MyTheme.Base">  
  8.     <item name="android:windowContentTransitions">true</item>  
  9.     <item name="android:windowAllowEnterTransitionOverlap">true</item>  
  10.     <item name="android:windowAllowReturnTransitionOverlap">true</item>  
  11.     <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>  
  12.     <item name="android:windowSharedElementExitTransition">@android:transition/move</item>  
  13.     <item name="android:windowTranslucentStatus">true</item>  
  14.   </style>  
  16. </resources>  

Step 5

In drawable Folder add Two Images with Name light_off and light_on that I attached with article files with name

 (Folder Name: drawable) 

(Files Name: light_off, light_on)

Xamarin Xamarin
Step 6

Now add another Folder in Resources Folder with the name raw, and in this Folder Click Button Sound with name sound_click that I attached. With name you can download from this article files.

(Folder Name: raw) 

(Files Name: sound_click)


Step 7

In Main,axml add this Code for ImageView and Toolbar, as shown below.

(Files Name: Main.axml)

XML Code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android=""  
  3.     xmlns:local=""  
  4.     android:orientation="vertical"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent">  
  8.     <  
  9.         android:layout_margin="10dp"  
  10.         android:id="@+id/toolbar"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:minHeight="?attr/actionBarSize"  
  14.         android:background="?attr/colorPrimary"  
  15.         android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"  
  16.         local:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>  
  18.     <LinearLayout  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="match_parent"  
  21.         android:layout_gravity="center">  
  22.         <ImageView  
  23.             android:id="@+id/imageView"  
  24.             android:layout_gravity="center"  
  25.             android:layout_width="match_parent"  
  26.             android:layout_height="wrap_content"  
  27.             android:src="@drawable/power_off" />  
  28.     </LinearLayout>  
  29. </LinearLayout>  


Step 8

In Solution Explorer Click on Properties and Open AndroidManifest.xml file. Add this Code for Permissions, as shown below.


Setting the permissions for our application

Like every single different process in a Linux situation, we initially need to set up the consents for our application to have the capacity to utilize. Without authorizations, our application will wind up having Runtime Exceptions. In this way, before going any further, open your application's show and compose the accompanying authorizations to it.

XML Code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="" package="CameraFlashLight.CameraFlashLight" android:versionCode="1" android:versionName="1.0">  
  3.     <uses-sdk android:minSdkVersion="16" />  
  4.     <uses-permission android:name="android.permission.CAMERA" />  
  5.     <uses-permission android:name="android.hardware.CAMERA" />  
  6.     <application android:label="CameraFlashLight"></application>  
  7. </manifest>  

The above code is the arrangement of consents required. We additionally need to be ready to utilize the blaze, in this manner we set them up independently. One thing to note is that in spite of the fact that everything that accompanies a camera is controlled by the camera, in any case, our application needs authorization to utilize every single segment including spotlight. Camera won't enable your application to locally speak with the parts if your show does not share the authorizations points of interest; consequently, a Runtime Exception.

The <uses-feature> component enables us to demonstrate that our application requires these highlights and is of no use without them. Obviously our application would be of no use, if there is no spotlight found in the gadget. The ascribe android:required enables us to set a condition for our application to be introduced, or overlook contingent upon the presence of that equipment highlight. You can set it to either obvious or false, indicating that the component is required.

That is, it for authorizations, we simply need to utilize the Camera protest and after that request that it turn on the electric lamp for us, as long as we need.

Step 9

In this step we write some functions and initialize some component of controls. Copy and Paste Code in to MainActivity.cs, as Shown below.

(Files Name: MainActivity.cs)


  1. using Android.App;  
  2. using Android.Widget;  
  3. using Android.OS;  
  4. using Android.Support.V7.App;  
  5. using Android.Hardware;  
  6. using System;  
  7. using static Android.Hardware.Camera;  
  8. using Java.Lang;  
  9. using Android.Util;  
  10. using Android.Media;  
  12. namespace CameraFlashLight  
  13. {  
  14.     [Activity(Label = "CameraFlashLight", MainLauncher = true,   
  15.         Icon = "@drawable/icon",Theme ="@style/MyTheme")]  
  16.     public class MainActivity : AppCompatActivity  
  17.     {  
  18.         //Initializing   
  20.         private Camera camera;  
  21.         private Parameters mParams;  
  22.         private bool isFlashLight;  
  23.         private ImageView btnFlash;  
  24.         private MediaPlayer player;  
  25.         protected override void OnCreate(Bundle bundle)  
  26.         {  
  27.             base.OnCreate(bundle);  
  29.             // Set our view from the "main" layout resource  
  30.             SetContentView (Resource.Layout.Main);  
  32.             //Set Title of App  
  33.             var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);  
  34.             SetSupportActionBar(toolbar);  
  35.             SupportActionBar.Title = "Flash Light";  
  37.             //FindView  
  39.             btnFlash = FindViewById<ImageView>(Resource.Id.imageView);  
  40.             player = MediaPlayer.Create(this, Resource.Raw.sound_click);  
  42.             //On Click delegate  
  44.             btnFlash.Click += delegate  
  45.             {  
  46.                 FlashLight();  
  47.             };  
  49.             //Has Flash Function For Checking  
  51.             bool hasflash = ApplicationContext.PackageManager.HasSystemFeature  
  52.             (Android.Content.PM.PackageManager.FeatureCameraFlash);  
  54.             if (!hasflash)  
  55.             {  
  56.                 Android.App.AlertDialog alert = new Android.App.AlertDialog.Builder(this).Create();  
  57.                 alert.SetTitle("Error!");  
  58.                 alert.SetMessage("Sorry, your device doesn't support flash light");  
  59.                 alert.SetButton("OK", (s, e) => { return; });  
  60.                 alert.Show();  
  61.             }  
  63.             getCamera();  
  64.         }  
  66.         private void getCamera()  
  67.         {  
  68.             if(camera == null)  
  69.             {  
  70.                 try  
  71.                 {  
  72.                     camera = Camera.Open();  
  73.                     mParams = camera.GetParameters();  
  74.                 }  
  75.                 catch (RuntimeException ex)  
  76.                 {  
  77.                     Log.Info("Error", ex.Message);  
  78.                 }  
  79.             }  
  80.         }  
  82.         //FlashLight Function  
  83.         private void FlashLight()  
  84.         {  
  85.             if (camera == null || mParams == null)  
  86.                 return;  
  87.             if (!isFlashLight)  
  88.             {  
  89.                 player.Start();  
  90.                 mParams = camera.GetParameters();  
  91.                 mParams.FlashMode = Parameters.FlashModeTorch;  
  92.                 camera.SetParameters(mParams);  
  93.                 camera.StartPreview();  
  94.                 isFlashLight = true;  
  95.                 btnFlash.SetImageResource(Resource.Drawable.power_on);  
  96.             }  
  97.             else  
  98.             {  
  99.                 player.Start();  
  100.                 mParams = camera.GetParameters();  
  101.                 mParams.FlashMode = Parameters.FlashModeOff;  
  102.                 camera.SetParameters(mParams);  
  103.                 camera.StartPreview();  
  104.                 isFlashLight = false;  
  105.                 btnFlash.SetImageResource(Resource.Drawable.power_off);  
  106.             }  
  107.         }  
  108.     }  
  109. }  


Now, run the Application and you will see the output given below

Xamarin  Xamarin


This is a very basic and easy method to create a flashlight app in Xamarin.Android using Visual Studio. Please share your comments and feedback.