Hi and welcome to another tutorial from CodingDemos :)
In this tutorial, you will learn how to turn ON/OFF the camera flashlight in Android programmatically.
Here are the steps:
1- Open up Android Studio.
2- Open activity_main.xml file and add an Android Switch view. This view will be used to switch ON/OFF the flashlight. Make sure that this view is disabled because you will enable it later based on certain conditions inside the MainActivity.java file.
Switch
android:id="@+id/switchFlashLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flash ON"
android:enabled="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
3- Open MainActivity.java file, here you will declare the Switch view and reference it based on their ID.
Switch flashControl = findViewById(R.id.switchFlashLight);
4- Next, you will use one of Android's Camera API called CameraManager. This will allow you to control the camera flashlight.
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
5- Before you can control the flashlight, you will first have to check if the device has a camera and a flashlight. If the device doesn't have either one, you will show an Android Toast message. If the device do have a camera with a flashlight, you will then enable the Android Switch view.
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
Toast.makeText(MainActivity.this, "This device has flash", Toast.LENGTH_SHORT).show();
flashControl.setEnabled(true);
} else {
Toast.makeText(MainActivity.this, "This device has no flash", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "This device has no camera", Toast.LENGTH_SHORT).show();
}
6- Next, you need to respond when the user clicks on the switch view by using the Android setOnCheckedChangeListener.
flashControl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
7- Inside onCheckedChanged, you will check the status of the Switch view. If it's checked, this means that the flashlight is ON, otherwise, it's OFF. In both conditions, you will show a Toast message.
if (isChecked) {
flashControl.setText("Flash ON");
} else {
flashControl.setText("Flash OFF");
}
8- You can turn ON the Android camera flashlight like this.
cameraManager.setTorchMode("0", true);
9- You can turn it OFF like this.
cameraManager.setTorchMode("0", false);
10- Make sure that you put the lines of code in steps 8 & 9 inside a Try & catch block. Otherwise, Android Studio will show an error below those lines.
try {
cameraManager.setTorchMode("0", false);
} catch (CameraAccessException e) {
e.printStackTrace();
}
11- Now build and run the app to see the result.
#androiddevelopment #androidprogramming #androidtutorial #androidstudio #androidstudiotutorial #java #codingdemos
Links:
Website: https://www.codingdemos.com
FaceBook: / codingdemos
Introduction 00:00
Final output: 00:25
Add Switch view in XML file 01:08
Add Switch in Java file 02:50
Check if device has a camera 05:40
Check if device has a flash 07:10
Turn flash ON/OFF 09:20
Conclusion 16:00