How to switch Torch or Flash Light on and off from your Android App? – complete source code

This video shows the steps to develop your Android App to control the Torch or Flash Light of the cameras of your Phone. In this tutorial it considers both back camera (camera ID = 0) and front camera (camera ID = 1) while toggling the flash light. It uses a very simple layout with 2 buttons to switch the flash light ON and OFF respectively. In the OnClick method of these buttons setTorchMode of respective cameras, front and back, is set to either true or false for switching it ON or OFF respectively.

For any feedback, suggestions or appreciation we will be glad to hear from you at: programmerworld1990@gmail.com

Source code:

Java Code File:

package com.example.torchflashlightcontroller;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

private CameraManager cameraManager;
private String cameraID;

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

cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
cameraID = cameraManager.getCameraIdList()[0]; // 0 is for back camera and 1 is for front camera
} catch (Exception e) {
e.printStackTrace();
}
}

public void TorchONButtonClick(View view){
try {
cameraManager.setTorchMode(cameraID, true);
} catch (Exception e) {
e.printStackTrace();
}
}

public void TorchOFFButtonClick(View view){
try {
cameraManager.setTorchMode(cameraID, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Manifest File:

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.torchflashlightcontroller”>

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

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

</manifest>

Layout XML file:

<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”154dp”
android:layout_marginTop=”125dp”
android:onClick=”TorchONButtonClick”
android:text=”@string/torch_on”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />

<Button
android:id=”@+id/button2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”155dp”
android:layout_marginTop=”99dp”
android:onClick=”TorchOFFButtonClick”
android:text=”@string/torch_off”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/button” />
</androidx.constraintlayout.widget.ConstraintLayout>

Gradle File:

apply plugin: ‘com.android.application’

android {
compileSdkVersion 29
buildToolsVersion “29.0.2”
defaultConfig {
applicationId “com.example.torchflashlightcontroller”
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName “1.0”
testInstrumentationRunner “androidx.test.runner.AndroidJUnitRunner”
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’
}
}
}

dependencies {
implementation fileTree(dir: ‘libs’, include: [‘*.jar’])
implementation ‘androidx.appcompat:appcompat:1.1.0’
implementation ‘androidx.constraintlayout:constraintlayout:1.1.3’
testImplementation ‘junit:junit:4.12’
androidTestImplementation ‘androidx.test.ext:junit:1.1.1’
androidTestImplementation ‘androidx.test.espresso:espresso-core:3.2.0’
}

Leave a Reply

%d bloggers like this: