Introduction: LED Flash Light Android Application
In this Instructable I will explain how to create free flashlight app for android. This will be a hand's on experience for the beginners in the Android Development. After following this tutorial you can create the best flashlight app for android phones and distribute it through google play.
Just before reading the instructable, please download the app to see the finished application from the following link Bright LED Flash Light App
I will be explaining very briefly here for more detailed explanation refer to following tutorial Android LED Flash Light Tutorial
I hope you have installed Android Studio. If you haven't then follow the tutorial Installing Android Studio. and then return here. Also before going through this tutorial we suggest you make a simple Hello World App and run it on your smartphone. You can learn How to Connect, Configure App on Real Android device and Run Hello World program with Android Studio . So now let's create our Led Flash Light Application. Start by creating a new project in Android Studio as follows.
Step 1: Creating a New Android Studio Project
Open Android Studio and create a new project by going to File => New => New Project. Enter the Application Name as LedFlashLight and your company domain name. (I have used my own company domain i.e androidtutorialpoint.com. so you can use yours).
Click Next and choose Minimum SDK. We have kept the default setting and click Next.
Choose Empty Activity and click next.
In the next screen enter Activity Name as FlashLightActivity and remember to check the Generate Layout Button and then click Finish.
Gradle will sync the project and resolve all the dependencies.
Step 5: Adding Permissions to Use Camera and FlashLight
Add Permissions in the Android Manifest.xml file to access Camera and FlashLight .Open your AndroidManifest.xml file and add the code from the attached AndroidManifest.xml file. Do remember to change the your domain name, since it should be globaly unique when you publish your app.
Step 6: Generate Application Layout
open activity_flash_light.xml and add the code from the attached activity_flash_light.xml file.
We are using an ImageButton. When the user presses this button the Led Flash Light will be toggled.
If you are new to android development kindly go through the following tutorial to understand more about generating layout Basics of Android Layout.
Also include the two image resources in your drawable folder under res
When the user clicks on the Turn On Button the Flash LED is turned on and the image toggles to the Turn Off State, similarly on clicking again Flash LED will turn off and the image toggle to Turn On State. We are also including the sound effect of user clicking the button.
Create a new folder rawres directory and add the flash_sound.mp3 file in that folder.
Step 7: Adding the Functionality
Add the code from attached FlashLightActivity.java file to your app's FlashLightActivity.java file.
Here we are just declaring the variables and in the onCreate() method we are setting the layout for the activity. We are also referencing the mTorchOnOffButton Button from the layout. We will talk more about this in a while. Here we are using Camera2 API since Camera API is deprecated in android now.
We need to detect whether the device has a Flash Light or not. In case the device doesn’t have support for flashlight h, you will get an error and your the apppp will close.
Next, we add the add the code to the onCreate() method to get the CameraManager object. Then we set the OnClickListener() for the on/off button for our Led Flash Light Application.
In the OnClickListener() we check whether the torch is currently on or off, then we call the turnOffFlashLight() to turn flash off in case the torch is already on and turnOnFlashLight() to turn flash on in case the torch is currently off.
Next we add the turnOffFlashLight() and turnOnFlashLight() methods for turning the Flash Off and On respectively, We will also add a method playOnOffSound to give the sound effect of clicking a button.
In the turnOffFlashLight() we turn off the Led Torch by setting
mCameraManager.setTorchMode(mCameraId, false);. Similarly in the turnOnFlashLight() we turn on the flashlight programmatically by setting mCameraManager.setTorchMode(mCameraId, true);. In the playOnOffSound() we use create() method of the MediaPlayer class to play the click sound.
At last override the Activity Lifecycle method's by adding the following code. When the App is minimized by user, we will turnOff the Flash and as soon as the user returns to the App, the Flash Light will resume if it was on earlier.
Step 8: Add App Icon & Run the App.
Add the following icon images in the mipmap-xxxhdpi , mipmap-xxhdpi, mipmap-xhdpi, mipmap-hdpi, mipmap-mdpi folders according to the size.
Now run the app on an Actual Device, turn on flashlight and then use your own brightest flashlight app to find you stuff in the dark. You can also install the torch light app from the following link Bright LED Flash Light. If you want to download the complete code for the app. Please visit the following link. Android LED Flash Light Tutorial.
I will soon write more such instructables to provide hand's on experience. Thanks guys for reading. If you have any doubts or suggestion then please leave comments in the comments section below . Subscribe to my website for latest Android Tutorials. Also do Like our Facebook Page or Add us on Twitter.
We have a be nice policy.
Please be positive and constructive.