FlashLight
Xml Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/toogleButton"
android:layout_width="120dp"
android:layout_height="106dp"
android:layout_centerInParent="true"
android:backgroundTint="@android:color/transparent"
android:src="@drawable/flashoff"
android:scaleType="centerCrop"/>
</RelativeLayout>
Java Code:
package com.example.gettoys;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ImageButton toogleButton;
boolean hasCameraFlash=false;
boolean flashOn=false;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toogleButton=findViewById(R.id.toogleButton);
hasCameraFlash=getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
toogleButton.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View view) {
if(hasCameraFlash){
if(flashOn){
flashOn=false;
toogleButton.setImageResource(R.drawable.flashoff);
try {
flashLightOff();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
else{
flashOn=true;
toogleButton.setImageResource(R.drawable.flash);
try {
flashLightOn();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}else {
Toast.makeText(getApplicationContext(),"No flahLight available on Your Device.",Toast.LENGTH_LONG).show();
}
}
});
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void flashLightOn() throws CameraAccessException {
CameraManager cameraManager=(CameraManager) getSystemService(Context.CAMERA_SERVICE);
String camerId=cameraManager.getCameraIdList()[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cameraManager.setTorchMode(camerId,true);
}
Toast.makeText(getApplicationContext(), "FlashLight is ON", Toast.LENGTH_SHORT).show();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void flashLightOff() throws CameraAccessException {
CameraManager cameraManager=(CameraManager) getSystemService(Context.CAMERA_SERVICE);
String camerId=cameraManager.getCameraIdList()[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cameraManager.setTorchMode(camerId,false);
}
Toast.makeText(getApplicationContext(), "FlashLight is Off", Toast.LENGTH_SHORT).show();
}
}