Android Torch Light / Flash Light App Tutorial

Android Torch Light / Flash Light App Tutorial

In this tutorial we are going to learn how to create android Torch light / Flash light app. This is one of the common utility apps you find in Google Play. If you have wondered how to develop this android app or you want to code your own android torch light app, this tutorial is for you.

The concept behind android Torch light app is that the app makes use of the device camera Flash Mode to turn on and off the camera flash.

This will not work on a device that does not support any camera flash but we will have to put a check in place so that our app will not end up crashing.

Before we start, the first thing I will do is to list the environment and tools I used in this android tutorial but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidFlashLightApp

Package: com.inducesmile.androidflashlightapp

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, make sure you change the package name if you did not use the same package.

Manifest.xml File

Since we are going to access and make use of some hardware resources in our device like camera, we are going to use some android permissions. The list is as follows but note that you can remove the one you free is not necessary

<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="" />

<uses-permission android:name="android.permission.FLASHLIGHT"/>

<uses-permission android:name="android.permission.WAKE_LOCK"/>

The Wake lock android permission is use to keep the screen alive when using our app. The updated version of our project Manifest.xml file is shown below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="" >
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="" />
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />



Open the main layout of your project, Since I used the default Main Activity, my corresponding layout for this page is activity_main.xml.

We are going to add an Imagebutton View in the layout file. In your IDE you can choose the layout design view and drag and drop an ImageButton to the layout.

If you prefer to use code view, then copy and paste the code below into your layout file. As you can see the layout is very simple. We will use the ImageButton View to turn on and off our flash light.

<RelativeLayout xmlns:android=""

        android:layout_marginTop="140dp" />
</RelativeLayout> file

In the class, we will instantiate the ImageButton by using the findViewById() method of the Activity class.

In order to fix the screen in portrait, we will call the method setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)  of the Activity class. Further more we will add a flag that will keep our app screen alive with getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)  method

In order to make sure that our app does do not use the Camera features in our device without first checking if our device supports Camera, we will use to following code to check if there is a flash light support in our device.

If there is none, we will use a Toast class to notify the use that the torch light will not work on the device.

boolean deviceHasFlash = getApplication().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

We will go further and create three private methods

1. turnOnTheFlash() – it is use to turn on the torch light

2. turnOffTheFlash() – it is use to turn off the torch light

3. getCamera() – use to return a Camera object

You can find the implementation of these method on the complete code of the class below.


import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    private ImageButton flashLight;
    private Camera camera;
    private Parameters parameter;
    private boolean deviceHasFlash;
    private boolean isFlashLightOn = false;

    protected void onCreate(Bundle savedInstanceState) {

        flashLight = (ImageButton)findViewById(;
        deviceHasFlash = getApplication().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
            Toast.makeText(MainActivity.this, "Sorry, you device does not have any camera", Toast.LENGTH_LONG).show();
            parameter =;

        flashLight.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

    private void turnOffTheFlash() {
        isFlashLightOn = false;

    private void turnOnTheFlash() {
        if( != null){
            parameter =;
            isFlashLightOn = true;

    private void getCamera() {
        if (camera == null) {
            try {
                camera =;
                parameter = camera.getParameters();
            } catch (RuntimeException e) {
                System.out.println("Error: Failed to Open: " + e.getMessage());

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == {
            return true;
        return super.onOptionsItemSelected(item);

    protected void onStop() {
        if( != null){
   = null;
    protected void onPause() {
    protected void onResume() {

    protected void onStart() {

Save the file and run your project. If everything works for you, the project will appear like this in your device.

Android Torch Light / Flash Light App Tutorial

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published

Add a Comment

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

50% Discounts On Projects

Do you have a custom project with a clear requirements, we are currently giving a 50% discount on all projects.

We will work with you to ensure we deliver what you need.

If you have questions or want to chat with us, you can reach us on Skype (henry.tochi)

Languages and technologies we use - Java, Kotlin, Laravel, Vue, React, Flutter, Swift and more.