Hello Guys,
In this tutorial , we will make you simple TorchLight App. we will create these app for android mobile using android studio with step by step. if you follow step carefully , so you will make your own android torchlight application with an easy method. TorchLight App MainScreen looking like below picture:
Let's get started.
Step 1 : Add Permission in Android-Manifest.xml
- Here you need to add features in Android-Manifest.xml file. Here camera.flash features is needed to add in Android-Manifest.xml file for making torchlight in mobile.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.torch">
<uses-feature android:name="android.hardware.camera.flash"/>
<application
android:allowBackup="true"
android:icon="@drawable/torch"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Torch">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 2 : activity_main.xml
- activity_main.xml file contain RelativeLayout which contain Button. Button is used to making torch on or off.
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_torch"
android:text="TORCH ON"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Step 3 : MainActivity.java
- Here we create CameraManager object. CameraManager object interact with camera services.
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.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btnTorch;
boolean IsTorchOn = false;
CameraManager cameraManager;
String cameraId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnTorch = findViewById(R.id.btn_torch);
//Check Torch Avaliable or not
boolean TorchAvailable = getApplicationContext().getPackageManager().
hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!TorchAvailable)
{
btnTorch.setEnabled(false);
btnTorch.setText("Torch Not Available");
}
try {
cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
cameraId = cameraManager.getCameraIdList()[0];
} catch (Exception e) {
e.printStackTrace();
}
btnTorch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(IsTorchOn)
{
//torch off here
IsTorchOn = false;
btnTorch.setText("TORCH ON");
}
else
{
//torch on here
IsTorchOn = true;
btnTorch.setText("TORCH OFF");
}
SwitchTorch(IsTorchOn);
}
});
}
public void SwitchTorch(boolean status)
{
try{
cameraManager.setTorchMode(cameraId,status);
}
catch (Exception e){
}
}
}
Output :
If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support. 🙏
0 Comments