Android FlashLight Torch Application 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...

  • Inz

    this code is not working

  • Inz

    when i run it on my laptop emulator its give me “Your device hardware does not support flashlight” and when i run it on my mobile it give me “app stopped” how can i solve these problems plz help me out

  • Dua Khan

    thanks.. Its Working for me..

  • Mahmoud Mosaad

    works fine, my advice for any one try this avoid any styling for button and do it with normal button and assure that the code works fine first then activate whatever u want on shape 🙂
    thanks for u 🙂

  • A – Kay Qureshi

    Thanks it works fine….
    but when we import android.hardware.Camera; it shows camera is depriciated…. in API23

  • hedi choura

    you can replace this if code
    ” if (isFlashSupported()) {
    camera = Camera.open();
    parameters = camera.getParameters();
    } else {
    showNoFlashAlert();
    }”

    by this code then it’s good for me
    “camera = Camera.open();
    parameters = camera.getParameters();”

    thanks a lot

  • Qunal Jain

    Thank you!!

  • zidd

    thanks it works!

  • Abdul Rehman

    sir thanks for the wondefull tutorial but this is not working in marshmallow 6.0 api 23 https://uploads.disquscdn.com/images/34b7cb9c19cbcccecd1f2d9894a3b3ac6c05389b00bfad5b845f15a0d9828b0d.png

    please sir update the code and give any other link to resolve problem

  • Pingback: tutorial video flashlight 1000 lumens cree – Confidence comes not from always being right()