Create Flashlight Application Android Studio

In this tutorial I will explain how Create Flashlight Application Android Studio platform. Nowadays every mobile has flashlight option and also available on Google Play Store. Here I share the procedure to make flashlight application in Android Studio. Here we don’t need any database like MySQL, MongoDB, Firebase etc for storing data from server. Because the work is just open the existing hardware tool of Flashlight. Which meas we have to enable the functionality via Java programming code.

In some mobile flashlight application is already build from device otherwise it will installed through play store. I assume you also one of the android developer that’s why here I post the article to make flashlight application. Current days most of mobile phone has flashlight feature on top notification bar location. Then we need this application ? Through the app we can do more benefits, that’s why everyone install those apps.

Create New Project

First as usual File -> Create New Project and choose some activities (for example Empty Activity), after selecting the activities the project will be created. The MainActivity.java file is automatically created,just open the file and add the following code below,

Most of top companies are earn lakhs of money via upload android apps on Google play store. So in your free time try to learn how deploy android apps with high demand for public. Because then only our application get more installations and reviews.

package io.github.sanbeg.flashlight;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewParent;
import android.widget.Toast;
import android.widget.ToggleButton;

public class FlashLightActivity extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener {
    private static final String TAG = "FlashLightActivity";
    private static final String LONG_PRESS = "long_press";
    public static final String WHITE = "white";
    private final Flash flash = new Flash();
    private View background;
    private ToggleButton theButton;
    private Drawable dark;
    private boolean changeColor = false;

    private SharedPreferences sharedPreferences;

    public class FlashTask extends AsyncTask {
        @Override
        protected Boolean doInBackground(Void... voids) {
            return flash.on();
        }
        @Override
        protected void onPostExecute(Boolean success) {
            theButton.setEnabled(true);
            if (! success) {
                Toast.makeText(FlashLightActivity.this, "Failed to access camera.", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public class WhiteTask extends AsyncTask {
        @Override
        protected Boolean doInBackground(Void... params) {
            return sharedPreferences.getBoolean(WHITE, false);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            changeColor = aBoolean;
            if (changeColor && theButton.isChecked()) {
                background.setBackgroundColor(Color.WHITE);
            } else {
                background.setBackgroundDrawable(dark);
            }
        }
    }

    public class PressTask extends AsyncTask {
        @Override
        protected Boolean doInBackground(Void... params) {
            return sharedPreferences.getBoolean(LONG_PRESS, false);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            background.setLongClickable(aBoolean);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        theButton = (ToggleButton) findViewById(R.id.flashlightButton);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        ViewParent vp = theButton.getParent();
        if (vp instanceof View) {
            background = (View) vp;
            background.setOnLongClickListener(new LongClickListener());
            dark = background.getBackground();
        } else {
            Log.e(TAG, "Background isn't a view!");
            background = new View(this);
        }

        ImageSpan imageSpan = new ImageSpan(this, R.drawable.power_symbol);
        SpannableString content = new SpannableString("X");
        content.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        theButton.setText(content);
        theButton.setTextOn(content);
        theButton.setTextOff(content);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Log.i(TAG, "Changed pref: " + key);
        switch (key) {
            case LONG_PRESS:
                new PressTask().execute();
                break;
            case WHITE:
                new WhiteTask().execute();
                break;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
            case R.id.settings:
                intent = new Intent(this, SettingsActivity.class);
                startActivity(intent);
                break;
            case R.id.action_about:
                intent = new Intent(this, AboutActivity.class);
                startActivity(intent);
                break;
        }
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        sharedPreferences.registerOnSharedPreferenceChangeListener(this);

        if (theButton.isChecked()) {
            theButton.setEnabled(false);
            new FlashTask().execute();
            theButton.setKeepScreenOn(true);
        } else {
            flash.off();
        }

        new PressTask().execute();
        new WhiteTask().execute();
    }

    @Override
    public void onPause() {
        super.onPause();
        sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
        flash.close();
    }

    public void onToggleClicked(View v) {
        if (theButton.isChecked()) {
            new FlashTask().execute();
            v.setKeepScreenOn(true);
            if (changeColor) {
                background.setBackgroundColor(Color.WHITE);
            }
        } else {
            flash.off();
            v.setKeepScreenOn(false);
            if (background != null) {
                background.setBackgroundDrawable(dark);
            }
        }
    }

    public class LongClickListener implements View.OnLongClickListener {
        @Override
        public boolean onLongClick(View view) {
            theButton.setChecked(!theButton.isChecked());
            onToggleClicked(theButton);
            return true;
        }
    }
}

XML Layouts

After writing the java we need to integrate XML file to build the Android Flshlight Application. For the example purpose here we explain just one page. And end of the article you can get full source code & clear documentation. So open the default file name of activity_main.xml file and add the following code below.

Also Read – Python Project Source Code Free of Cost

<?xml version="1.0" encoding="utf-8"?>
<ToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flashlightButton"
    android:layout_width="128dp"
    android:layout_height="192dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:onClick="onToggleClicked" />

Download Source Code

In below I have add the link for how create Flashlight application android studio platform. Already we are checking this code in our system and it’s working fine without any issues. So you can execute on your laptop & if facing any error just contact us for clarify bugs.

Leave a Reply