Android FlashLight Torch App Development Tutorial

With help of android Camera and Camera.Parameters, we will develop Android Flashlight Torch Application.

 

 

FlashLight Application

1. Preparing Application Manifest File

For accessing Camera, we need to declare uses-permission to use it along with uses-feature.

2. Preparing UI Components

Our applications main activity layout will have ImageButton for switching on/off of flashlight. Modify your /res/layout/activity_main.xml as below.

For designing ImageButton image resource with 3D effect, I have opted layer-list drawable from this stackoverflow by savanto.

We will two create drawable for on and off torch state.

/res/drawable/flashlight_off.xml

/res/drawable/flashlight_on.xml

3. Preparing Source Files

In our application launcher activity MainActivity.java, we will use a boolean variable to toggle between Camera flash state. We need to check first if flash feature PackageManager.FEATURE_CAMERA_FLASH is supported by your device hardware. Make sure to set View.OnClickListener for your ImageButton. On Clicking of ImageButton, if flash light is OFF we will turn it on by setting paramters Parameters.FLASH_MODE_TORCH and calling startPreview(). If flash light is ON, we will turn it off setting Parameters.FLASH_MODE_OFF and call stopPreview(). We need to make sure to release camera if held while exiting activity.

4. Building and Running Application

Now build and run your applications, you are done with Android FlashLight Torch Application using Camera and Camera.Parameters.

You may also like...