We’ve seen a lot of flashlight application in the Google Playstore and it’s fairly a lot helpful to Android users. In this tutorial, I am going to teach you how to create a simple flashlight application and how to design your button’s UI using an image view.

Let’s start by creating a new blank project in Android Studio and by adding these lines in your AndroidManifest.xml.

This uses-permission tag tells the Android OS that our app will be needing to access it’s camera and the uses-feature tag tells what feature will be used by our application. Make sure to include these lines so that our application will run smoothly.

 

We also need to remove the default action bar so that our application will look like this.

We can do that by editing styles under res>values folder. Open styles.xml and change your current style to this.

 

We also need to make our application limited to portrait view. We can do that by adding this line in the activity tag in our AndroidManifest.xml

Now that we’ve added all the necessary changes in our application, we can start creating our flashlight application.

Open activity_main.xml and add the code provided below.

 

You can download the image resource here or you can provide your own. Make sure to add the image resource under drawable folder.

 

 

Add these as global variables in your MainActivity.java

Now open MainActivity.java and add these methods after your onCreate method.

 

This will open and get the parameters of the camera.

 

These will control the camera’s flash.

 

These will handle the camera when the activity’s life cycle changes. For example, when we closes the application the camera flash will automatically be closed. This is very important because we don’t want our camera’s flash to remain turned on as it will drain our device’s battery or leave a permanent damage to our device.

 

Now copy and paste these codes inside your onCreate method.

Now you can run and test you app if it is working. The final output of this tutorial should be like this.

 

 

 

 

 

 

 

You can download the full source code here: Download Link