Turn on Flashlight Android – Building a Torch App

Hey everyone, this is a tutorial for the newbies, the people just started getting there hands dirty on android coding. I thought I would post some small apps tutorial for you all, so that you …

turn on flashlight android

Hey everyone, this is a tutorial for the newbies, the people just started getting there hands dirty on android coding. I thought I would post some small apps tutorial for you all, so that you can enjoy the learning. Today, in this post we will learn how to Turn on Flashlight in Android using Code. As the title says “Turn on Flashlight Android – Building a Torch App”, so we are going to build a simple torch application today.

Torch Application Android Project

  • The first thing, as always we need our new project. So open Android Studio and create a new project with an Empty Activity.
turn on flashlight android
Turn On Flashlight Android
  • Make sure you select API 23, as the minimum API. This is because the API that we are going to use is available on API 23 and later only, and the older one is deprecated. So to make things clean and easy to understand we are targeting only API 23 and later.

Creating Interface

  • Here you can play with the design, you can put nice images and you can make your screen look awesome. But the fun part is I am not going to bother about design, and yes I will create probably the worse UI. haha .
  • So come inside activity_main.xml and write the following code.

  • The above XML will give you the following output. We only have a ToggleButton that we will use to turn on or off the android flash light.
turn on flashlight android
Turn on Flashlight Android
  • So here we have one of the worse UI design of all time. 😉 LOL. Design if you want, or lets dive into adding the functionality.

Adding Feature in Manifest

  • As we are going to build a Torch application, and basically we want to programmatically turn on the flashlight of our android device. To do this we need to access this feature of android.
  • So come inside AndroidManifest.xml and add the line shown below.

Building Basic Code Structure

  • We don’t have much to do, we only have a ToggleButton where we need to attach a listener to listen for the on and off events. To do this we will write the following code.

Checking if Flash is Available

  • Now let’s write some java code. When our application will enter the onCreate() method, first we will check if the device has a flash light or not. If the device do not have flashlight we will quit the application saying the flash is not available.
  • So our onCreate() method will be like this.

  • We are calling the method showNoFlashError() that you can define as below.

Getting Camera Manager

  • Now we need CameraManager, and a String for the Camera Id, as most devices has multiple cameras.

  • Now inside onCreate() we will get the CameraManager and Camera Id.

  • This time you can also see that inside the ToggleButton Listener we are calling a method named switchFlashLight() and we are passing the isChecked boolean variable to this method which tells the current state of the Toggle Button, whether it is On or Off.
  • This method switchFlashLight() will do the actual task so let’s define it.

Turn on Flashlight Android

  • The code is very simple only a single line as we already defined the required objects.

  • Now you can try running your application.
  • If you are having any confusions, then here is the full code of our MainActivity.java.

So that is all for this Turn On Flashlight Android tutorial guys. I hope you found it helpful. If you have any query, just leave that in the comment section below. And don’t forget to share this post with android newbies. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x