07 July 2021
Toggle the flash mode with Camera X

The Problem

The CameraX library goes a long way towards making the camera API’s in Android bearable to work with.

However, it’s a relatively new library at the time of writing this article, the documentation is somewhat lacking, and the API changes frequently.

A question I’ve seen pop up a few times is: How do I toggle the flash mode?

The Solution

If you’ve followed a tutorial, you should end up with an ImageCapture object, typically configured something like this:

imageCapture = ImageCapture.Builder()

As you can see, setting the flash mode at this point is trivial.

To change the flash mode, you need to use:

imageCapture.flashMode = ImageCapture.FLASH_MODE_OFF

Typically, you can do this in the OnClickListener of a button.