Android Flashlight Torchlight Tutorial

Nowadays, all smart phones have torchlight or flashlight capability which come with the camera. The torchlight make the camera able to capture photos and make us able to viewing the objects in the dark environment. It is a very good features and provide the good user experience to the smart phones user. In this tutorial, I will give you some tutorial on how to create a flashlight application in android studio.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “FlashLightExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Edit activity_main.xml layout

In activity_main.xml add two button in the layout. The first button is turn on the flash light while the second button is turn off the flash light.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btnSwitchOn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ON"


        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:id="@+id/btnSwitchOff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OFF"
        android:layout_below="@+id/btnSwitchOn"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />



</RelativeLayout>

Edit MainActivity.java class

Go to your MainActivity.java and copy paste the following code. hasFlash boolean is to indicate the android device support flash light anot.

package com.example.questdot.flashlightexample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    Button btnSwitchOn,btnSwitchOff;

    private Camera camera;

    private boolean hasFlash;
    Parameters params;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnSwitchOn = (Button) findViewById(R.id.btnSwitchOn);
        btnSwitchOff = (Button) findViewById(R.id.btnSwitchOff);

  
        hasFlash = getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

        if (!hasFlash) {
       
            Toast.makeText(getApplicationContext(),"Your device does not support flash light",Toast.LENGTH_LONG).show();
            
        }

        btnSwitchOn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                    turnOnFlash();

            }
        });

        btnSwitchOff.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

               turnOffFlash();

            }
        });
    }

 
    private void turnOnFlash() {

            camera = Camera.open();
            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();




    }


    private void turnOffFlash() {


            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();



            camera.release();

    }

    @Override
    protected void onPause() {
        super.onPause();
        turnOffFlash();
    }


    @Override
    protected void onResume() {
        super.onResume();

        // on resume turn on the flash
        if(hasFlash)
            turnOnFlash();
    }


}

Run your Project

Now, you can run your project in your smartphone to test out the flash light.

(Android Flashlight Torchlight)

Source Code

(Visited 765 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *