Make Android FlashLight App Using Android Studio

Please log in or register to like posts.
News

Make Android FlashLight App Using Android Studio

flashlight app

Hi Android Studio Beginners, Today I’m going to show you how to make android flashlight app using android studio. So Guys, It’s a interesting way to make android flashlight application. even, carefully read this tutorial and finish all of the step using instructions and your mind also. Some Friends was told me to write a article about android studio flashlight application development. Friends Today I’m Still Giving Methods and Instructions Below.

FlashLight App

First You Need To Start A New Project From Android Studio. Make a Application With Blank Activity. Suppose, LedFlashLight The App Name. You Can Write Any Name Of Your Project. Some More Basic Android Studio Development Tutorials. For Android Simple App Development Instructions.Well, Now We Are Going To Start Making Android FlashLight Application  Via Android Studio.

 

First Open AndroidManifest.xml FIle And Give Below Permission Via Below Code.

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.flash" />

These uses-permissions tag tells the Android OS that our app will require access to CAMERA and FLASHLIGHT. Similarly uses-feature tells what features will be used in the app. The Complete AndroidManifest.xml will be look like below code…

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.androidtutorialpoint.ledflashlight" >
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.flash" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".FlashLightActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Apart from the package name everything should be same for you.and now open activity_flash_light.xml and add the following code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.camera2.MainActivity"
    android:background="#000"
    android:gravity="center">
 
    <ImageButton
        android:layout_gravity="center"
        android:id="@+id/button_on_off"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000"
        android:src="@drawable/off"/>
 
</LinearLayout>

We are using an ImageButton. When the user presses this button the Led FlashLight will be toggled. Upload Your Turn Of and On Image in Drawable Folder. Now We Need TO Configure FlashLightActivity.java File And Make This App Working. Open FlashLightActivity.java File and Copy Past The Below Code.

package com.howtrick.ledflashlight;
 
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
 
 
public class FlashLightActivity extends AppCompatActivity {
 
    private CameraManager mCameraManager;
    private String mCameraId;
    private ImageButton mTorchOnOffButton;
    private Boolean isTorchOn;
    private MediaPlayer mp;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("FlashLightActivity", "onCreate()");
        setContentView(R.layout.activity_flash_light);
        mTorchOnOffButton = (ImageButton) findViewById(R.id.button_on_off);
        isTorchOn = false;

Remember, replace package name with your given website name. it was generated, Just Ignore 1st Line Code and Copy Past On Your Screen After Package Name Line.

[ads]

Here we are just declaring the variables and in the onCreate() method we are setting the layout for the activity. We are also referencing the mTorchOnOffButton Button from the layout. We will talk more about this in a while. Here we are using Camera2 API since Camera API is deprecated in android now. Replace The Below Code Into FlashLightActivity.java file after last line.

Boolean isFlashAvailable = getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
 
        if (!isFlashAvailable) {
 
            AlertDialog alert = new AlertDialog.Builder(FlashLightActivity.this)
                    .create();
            alert.setTitle("Error !!");
            alert.setMessage("Your device doesn't support flash light!");
            alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // closing the application
                    finish();
                    System.exit(0);
                }
            });
            alert.show();
            return;
        }

If your phone doesn’t support camera flash, you will get the following error.
on pressing the OK button the app will close.

Next, we add the add the code to the onCreate() method to get the CameraManager object. Then we set the OnClickListener() for the on/off button for our Led FlashLight Application.Now We Need To Add This Below Code Into FlashLightActivity.java file.

mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            mCameraId = mCameraManager.getCameraIdList()[0];
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
 
        mTorchOnOffButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (isTorchOn) {
                        turnOffFlashLight();
                        isTorchOn = false;
                    } else {
                        turnOnFlashLight();
                        isTorchOn = true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

For Turning On and Off Options We Need To Make turnOffFlashLight() and turnOnFlashLight() methods for turning the Flash Off and On respectively, We will also add a method playOnOffSound to give the sound effect of clicking a button.

[ads]

So Now, We are Going To Make Some Code That Can Control On Off Switch Options. Replace This Below Code On FlashLightActivity.Java File.

public void turnOnFlashLight() {
 
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mCameraManager.setTorchMode(mCameraId, true);
                playOnOffSound();
                mTorchOnOffButton.setImageResource(R.drawable.on);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    public void turnOffFlashLight() {
 
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mCameraManager.setTorchMode(mCameraId, false);
                playOnOffSound();
                mTorchOnOffButton.setImageResource(R.drawable.off);
 
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void playOnOffSound(){
 
        mp = MediaPlayer.create(FlashLightActivity.this, R.raw.flash_sound);
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
 
            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.release();
            }
        });
        mp.start();
    }

Now We Are Making A Function This Can Work Look Like Android Sensor. When FlashLight Will Be Open It Can Working. When User Lock There Phone By Clicking Lock Button It Will Be Turn Off.When the App is minimized by user, we will turnOff the Flash and as soon as the user returns to the App, the Flash Light will resume if it was on earlier. Hope This Function Will Give You Something. Replace The Below Code Into FlashLightAcctivity.Java File.

@Override
    protected void onStop() {
        super.onStop();
        if(isTorchOn){
            turnOffFlashLight();
        }
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        if(isTorchOn){
            turnOffFlashLight();
        }
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        if(isTorchOn){
            turnOnFlashLight();
        }
    }
}

Congratulations!! Now Your Android FlashLight App Is Ready To Run. Build This App Or Debug To Your Android Phone Via USB Cable and Look It Working Or Not. Hope It Can Be Working, Because Of This Tutorial Was Tested This Method. You Can Publish Your Android FlashLight App One Google Play Store Or Online Apps Website.

[ads]

Nobody liked ?

Leave a Reply

Your email address will not be published. Required fields are marked *