Search

A Flash Light App in Sketchware

To create a Torch Flashlight application for Android with Sketchware follow the steps given below.

1. Create a new project in Sketchware. In VIEW area add an ImageView imageview1. Set it's width and height to 100, and scale type to FIT_XY.

2. Using Image Manager add two images ic_flash_on_black and ic_flash_off_black.

3. Set ic_flash_off_black as the image of imageview1.


4. In Library manager switch on AppCompat and Design.

5. Add a Camera component.

6. Add two Boolean variables: flashLightStatus and hasCameraFlash.

7. Add two More Blocks: flashLightOn and flashLightOff.


8. In onCreate event, use add source directly block and put following code:
hasCameraFlash = getPackageManager(). hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

9. In More Block flashLightOn, use add source directly block and put following code:
android.hardware.camera2.CameraManager cameraManager = (android.hardware.camera2.CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0]; cameraManager.setTorchMode(cameraId, true);
flashLightStatus = true; imageview1.setImageResource(R.drawable.ic_flash_on_black); } catch (android.hardware.camera2.CameraAccessException e) { }

10. In More Block flashLightOff, use add source directly block and put following code:
android.hardware.camera2.CameraManager cameraManager = (android.hardware.camera2.CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0]; cameraManager.setTorchMode(cameraId, false);
flashLightStatus = false; imageview1.setImageResource(R.drawable.ic_flash_off_black); } catch (android.hardware.camera2.CameraAccessException e) { }

11. Add imageview1 onClick event. Here use blocks as shown in image below.
12. Save and run the project. In the app, click the ImageView to switch on flash light.


Create Flashlight App using Switch
1. Create a new project in Sketchware. In VIEW area add a Switch switch1. Set it's width to match_parent, and gravity to left.


2. In Library manager switch on AppCompat and Design.

3. Add a Camera component.

4. Add two boolean: flashLightStatus
and hasCameraFlash.

5. Add two More Blocks:
 flashLightOn and flashLightOff.


6. In onCreate event, use add source directly block and put following code:
hasCameraFlash = getPackageManager(). hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

7. In More Block flashLightOn, use add source directly block and put following code:
android.hardware.camera2.CameraManager cameraManager = (android.hardware.camera2.CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0]; cameraManager.setTorchMode(cameraId, true);
flashLightStatus = true; } catch (android.hardware.camera2.CameraAccessException e) { }

8. In More Block flashLightOff, use add source directly block and put following code:
android.hardware.camera2.CameraManager cameraManager = (android.hardware.camera2.CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0]; cameraManager.setTorchMode(cameraId, false);
flashLightStatus = false; } catch (android.hardware.camera2.CameraAccessException e) { }

9. Add switch1 onCheckChanged event. Here use blocks as shown in image below.

10. Save and run the project. In the app, click the Switch to switch on flash light.

Post a Comment

0 Comments