How to make a Flash light Application in Android Studio without any dependencies



    Today's post is about how to build a flashlight app in Android Studio. In this tutorial, we'll show you how to make a flashlight application for Android using Android Studio. As a result, if you closely follow my instructions, you will be able to create your own Android flashlight application using a simple procedure.

 

 
 

Download images: https://www.mediafire.com/folder/5l7r8djm3ue7i/flashlight
 

 

Step 1 : Create an Android Studio Project in Android Studio. Download the Source Pictures Below and Paste it in the Resource/drawable.

(note: Android version should be Lollipop or above)

 

Step 2 : Add permissions in AndroidManifest.xml page


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.torch">
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera2"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <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 3 : Design the activity_main.xml page

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="@drawable/off"
    android:id="@+id/cl"
    tools:context=".MainActivity">



</androidx.constraintlayout.widget.ConstraintLayout>

 

Step 4 : Write the functions in MainActivity,java page

MainActivity.java

package com.example.torch;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    ConstraintLayout layout;
    CameraManager cameraManager;
    String cameraId;
    Boolean state=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layout=findViewById(R.id.cl);
        layout.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {

                if(state == false){
                    try{
                        cameraManager= (CameraManager) getSystemService(CAMERA_SERVICE);
                        cameraId=cameraManager.getCameraIdList()[0];
                        cameraManager.setTorchMode(cameraId,!state);
                        layout.setBackgroundResource(R.drawable.on);
                        state=true;

                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }
                else {
                    try {
                        cameraManager= (CameraManager) getSystemService(CAMERA_SERVICE);
                        cameraId=cameraManager.getCameraIdList()[0];
                        cameraManager.setTorchMode(cameraId,!state);
                        layout.setBackgroundResource(R.drawable.off);
                        state=false;
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}

 
video Tutorial 

 
For more APP DEVELOPMENT VIDEOS vist our channel:
https://www.youtube.com/channel/UCnKT1wmNUa_gQwDDNJVbU4g?sub_confirmation=1 
 
 

 


Comments