MainActivity.java:

package com.example.myapplication;

import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    ToggleButton flashlightToggle;
    ImageButton flashlightImageButton;
    TextView statusTextView;

    CameraManager cameraManager;
    String cameraId;

    boolean isFlashlightOn = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        flashlightToggle =
                findViewById(R.id.flashlightToggle);

        flashlightImageButton =
                findViewById(R.id.flashlightImageButton);

        statusTextView =
                findViewById(R.id.statusTextView);

        cameraManager =
                (CameraManager) getSystemService(CAMERA_SERVICE);

        // FIX: pick the camera that actually has a flash unit
        try {
            for (String id : cameraManager.getCameraIdList()) {
                Boolean hasFlash = cameraManager.getCameraCharacteristics(id)
                        .get(CameraCharacteristics.FLASH_INFO_AVAILABLE);

                if (hasFlash != null && hasFlash) {
                    cameraId = id;
                    break;
                }
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }

        if (cameraId == null) {
            Toast.makeText(this,
                    "Flashlight not available on this device",
                    Toast.LENGTH_LONG).show();
            flashlightToggle.setEnabled(false);
            flashlightImageButton.setEnabled(false);
        }


        // ToggleButton

        flashlightToggle.setOnCheckedChangeListener(
                (buttonView, isChecked) -> {

                    isFlashlightOn = isChecked;

                    turnFlashlight(isChecked);
                }
        );


        // ImageButton

        flashlightImageButton.setOnClickListener(v -> {

            // FIX: the toggle's listener turns the torch on/off (no double call)
            flashlightToggle.setChecked(!flashlightToggle.isChecked());
        });
    }


    // Turn flashlight ON/OFF

    private void turnFlashlight(boolean status) {

        if (cameraId == null) {
            return;
        }

        try {

            cameraManager.setTorchMode(
                    cameraId,
                    status
            );

            if (status) {



                statusTextView.setText("Flashlight ON");

            } else {

                statusTextView.setText("Flashlight OFF");
            }

        } catch (CameraAccessException | IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
}


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="30dp">

    <TextView
        android:id="@+id/statusTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Flashlight OFF"
        android:textSize="25sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_marginBottom="30dp" />

    <ToggleButton
        android:id="@+id/flashlightToggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="FLASHLIGHT ON"
        android:textOff="FLASHLIGHT OFF"
        android:layout_marginBottom="30dp" />

    <ImageButton
        android:id="@+id/flashlightImageButton"
        android:layout_width="100dp"
        android:layout_height="100dp"

        android:src="@drawable/flashlight"
        android:scaleType="centerInside"
        android:background="@android:color/transparent"
        android:contentDescription="Flashlight" />

</LinearLayout>


AndroidManifest.xml:

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

    <uses-feature
        android:name="android.hardware.camera.flash"
        android:required="false" />



    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
