Flashlight App Android Example

In this tutorial I will teach you to make a very basic and simple flashlight app for android. This application lets you turn on and off your phone camera’s LED flash. I have tested this app in some Micromax and Karbonn mobiles.

Also Read: Simple Notepad App Android Example
Also Read: How to Make a Calculator App for Android

 

Flashlight App Android Example

 

Flashlight App Android Example

Create a new project with package name com.thecrazyprogrammer.www.flashlight. You can change it according to you, but make sure to change the package name in the main code.

MainActivity.java

package com.thecrazyprogrammer.www.flashlight;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;

public class MainActivity extends Activity {

    Button onBtn,offBtn;
    Camera cam;
    Parameters p;
    boolean status;

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

        onBtn=(Button)findViewById(R.id.onBtn);
        offBtn=(Button)findViewById(R.id.offBtn);

        onBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!status)
                {
                    cam = Camera.open();
                    p = cam.getParameters();
                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    cam.setParameters(p);
                    cam.startPreview();
                    status=true;
                }
            }
        });

        offBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(status) {
                    cam.stopPreview();
                    cam.release();
                    status=false;
                }
            }
        });
    }
}

 

activity_main.xml

<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ON"
        android:id="@+id/onBtn"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="OFF"
        android:id="@+id/offBtn"/>

</LinearLayout>

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thecrazyprogrammer.www.flashlight" >

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>

 

Explanation

Turn On Flashlight

The following code turn on the flashlight.

cam = Camera.open();
p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();

 

Camera is a class used to perform all functionality related to camera.

 

Turn Off Flashlight

The following code turn off the flashlight.

cam.stopPreview();
cam.release();

 

Camera Permission

Make sure to add following two lines in AndroidManifest.xml file to get access to camera device.

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

 

You can test this app in your mobile by downloading the apk from below link.

– Download Flashlight Apk –

 

If you found anything incorrect or have doubts regarding above flashlight app android example tutorial then comment below.

11 thoughts on “Flashlight App Android Example”

  1. Bro can u tell me in brief about this??
    I m a beginner anddon’t know about which app or software to use for this type of coding.
    And also I didn’t understand this coding
    I don’t know much of the things
    Can u help??
    Plzz

  2. Hello Crazy Programmer!
    I’m working on and android app mixing android and openframeworks and i need to use android 21 and this .camera is depreciate know you have to use .camera2 that already contain a function for torch light and you don’t need to open the camera anymore. I haven’t find any tutorial or good reference to do this did you already use it or you have a tutorial i will appreciate any help. Thanks.

      1. sir i think that the marshmallow runtime permisson is needed in api 23 6.0+ which is not added plz add this permissons thanks

  3. hy!
    can u plz tell me on which platform i can run this code?? so that i don’t have to download it.
    as i do coding in java on bluej!!

  4. ubaid mohamed dahir

    I tried this code and it is not displays an error but when i used it to compile my phone it says “flash has stopped”

Leave a Comment

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