How To Create Flashlight App Android
Today i am going to demonstrate developing a simple android application. This application is about turning on and off device flashlight / torchlight with simple touch. Please note that this app is targeted to mobile devices only, i am not sure how this app looks in tablet devices.
Designing the application
This app contains not too complex design. You can design the application using graphic editor software like photoshop. Please find the PSD of the app in the source code folder of this tutorial. I am not covering the steps involved in designing the application as it is not the part of the tutorial.
Thanks to Keith Sereby for providing beautiful switch image. Check out his profile on dribble and twitter
Following is the screenshot of the app which we are going to develop in this tutorial.
Resize and save each switch image in multiple dimensions for both on and off states. See the following image for your reference
Once you are done with designing part you can move to coding part by starting a new project.
Creating new project
1. Create a new project in Eclipse IDE by going to File ⇒ New ⇒ Android Application Project . I named my package as com.androidhive.flashlight and main activity as MainActivity.java
2. Open your AndroidManifest.xml file and add required permissions. I also disabled landscape mode in manifest file by using android:screenOrientation="portrait" property.
android.permission.CAMERA – required to access the camera device
android.hardware.camera – required to access camera hardware features
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidhive.flashlight" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.androidhive.flashlight.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
3. Place all the switch image files under drawable-hdpi, drawable-mdpi, drawable-ldpi folders. I named switch image files as btn_switch_off.png and btn_switch_on.png
4. Create a new xml file named radial_background.xml under res ⇒ drwable-hdpi folder and paste the following code. This file is used to create radial gradient background for the app.
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:type="radial" android:startColor="#343030" android:endColor="#151515" android:gradientRadius="300" android:angle="270" android:centerY="0.3"/> </shape>
5. Now open activity_main.xml file located under res ⇒ layout folder and type the following code. This layout file acts as main screen of the application.
In the following code first we applied a gradient background using radial_background.xml file which we created earlier. After that we placed an Image button with the switch image.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/radial_background" tools:context=".MainActivity" > <ImageButton android:id="@+id/btnSwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="100dip" android:src="@drawable/btn_switch_on" android:background="@null" android:contentDescription="@null" /> </RelativeLayout>
6. Open your MainActivity.java file and do the following changes. Here we just declared required variables.
package com.androidhive.flashlight; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageButton; public class MainActivity extends Activity { ImageButton btnSwitch; private Camera camera; private boolean isFlashOn; private boolean hasFlash; Parameters params; MediaPlayer mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // flash switch button btnSwitch = (ImageButton) findViewById(R.id.btnSwitch); }
Detecting whether device supporting flashlight or not
The important step is to notify user if the device doesn't support flashlight by showing an alert message or some other means, then we'll simply close the application.
7. Open MainActivity.java and add the following code. In the following code we are checking availability of the flashlight and if the device doesn't support it, we'll show alert dialog and close the application by calling finish().
/* * First check if device is supporting flashlight or not */ hasFlash = getApplicationContext().getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); if (!hasFlash) { // device doesn't support flash // Show alert message and close the application AlertDialog alert = new AlertDialog.Builder(MainActivity.this) .create(); alert.setTitle("Error"); alert.setMessage("Sorry, your device doesn't support flash light!"); alert.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // closing the application finish(); } }); alert.show(); return; }
Turning on Flashlight
8. Turning on flashlight can be done by setting camera flash mode to FLASH_MODE_TORCH. The following two functions getCamera() and turnOnFlash() will do that for us. Add these functions to your MainActivity.java file
// getting camera parameters private void getCamera() { if (camera == null) { try { camera = Camera.open(); params = camera.getParameters(); } catch (RuntimeException e) { Log.e("Camera Error. Failed to Open. Error: ", e.getMessage()); } } } /* * Turning On flash */ private void turnOnFlash() { if (!isFlashOn) { if (camera == null || params == null) { return; } // play sound playSound(); params = camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.startPreview(); isFlashOn = true; // changing button/switch image toggleButtonImage(); } }
Turning off Flashlight
9. Flashlight can be turned off by setting flash mode to FLASH_MODE_OFF.
/* * Turning Off flash */ private void turnOffFlash() { if (isFlashOn) { if (camera == null || params == null) { return; } // play sound playSound(); params = camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(params); camera.stopPreview(); isFlashOn = false; // changing button/switch image toggleButtonImage(); } }
Finally call these functions in button click event
/* * Switch click event to toggle flash on/off */ btnSwitch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (isFlashOn) { // turn off flash turnOffFlash(); } else { // turn on flash turnOnFlash(); } } });
Toggle switch images on flashlight on / off
10. When user touches the switch we need to toggle the switch image to on and off state just to indicate flashlight status. toggleButtonImage() has been called in turnOnFlash() and turnOffFlash() functions.
/* * Toggle switch button images * changing image states to on / off * */ private void toggleButtonImage(){ if(isFlashOn){ btnSwitch.setImageResource(R.drawable.btn_switch_on); }else{ btnSwitch.setImageResource(R.drawable.btn_switch_off); } }
Playing switch sound
If you are done with all the previous steps, your app will work perfectly. Here we can add an enhancement to our app by playing switch sound when user touches the switch. I downloaded two sound files from free sound libraries for switch on / switch off states and named them as light_switch_on.mp3 and light_switch_off.mp3
11. Under res folder create a new folder called raw and place these sound files in it. (res ⇒ raw ⇒ light_switch_on.mp3, light_switch_off.mp3)
12. Add the following function playSound() to your MainActivity.java file.
This function has been called in turnOnFlash() and turnOffFlash() functions.
/* * Playing sound * will play button toggle sound on flash on / off * */ private void playSound(){ if(isFlashOn){ mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off); }else{ mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on); } mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub mp.release(); } }); mp.start(); }
13. Importantly don't forgot to release the camera once our app entered into background state as other apps might need to access camera. Add following code to your MainActivity.java file.
In onPause() method we turned off the flashlight as app entered in background state
In onResume() method we turned on the flashlight back again
In onStop() method we released the camera by calling camera.release() method.
@Override protected void onDestroy() { super.onDestroy(); } @Override protected void onPause() { super.onPause(); // on pause turn off the flash turnOffFlash(); } @Override protected void onRestart() { super.onRestart(); } @Override protected void onResume() { super.onResume(); // on resume turn on the flash if(hasFlash) turnOnFlash(); } @Override protected void onStart() { super.onStart(); // on starting the app get the camera params getCamera(); } @Override protected void onStop() { super.onStop(); // on stop release the camera if (camera != null) { camera.release(); camera = null; } }
Setting the app icon
Finally design an icon for the app and place the image in drawable-hdpi (72 x 72 px), drawable-mdpi (48 x 48 px) and drawable-ldpi (36 x 36 px) folders.
Final Code
Final code of MainActivity.java is as follows
package com.androidhive.flashlight; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageButton; public class MainActivity extends Activity { ImageButton btnSwitch; private Camera camera; private boolean isFlashOn; private boolean hasFlash; Parameters params; MediaPlayer mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // flash switch button btnSwitch = (ImageButton) findViewById(R.id.btnSwitch); // First check if device is supporting flashlight or not hasFlash = getApplicationContext().getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); if (!hasFlash) { // device doesn't support flash // Show alert message and close the application AlertDialog alert = new AlertDialog.Builder(MainActivity.this) .create(); alert.setTitle("Error"); alert.setMessage("Sorry, your device doesn't support flash light!"); alert.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // closing the application finish(); } }); alert.show(); return; } // get the camera getCamera(); // displaying button image toggleButtonImage(); // Switch button click event to toggle flash on/off btnSwitch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (isFlashOn) { // turn off flash turnOffFlash(); } else { // turn on flash turnOnFlash(); } } }); } // Get the camera private void getCamera() { if (camera == null) { try { camera = Camera.open(); params = camera.getParameters(); } catch (RuntimeException e) { Log.e("Camera Error. Failed to Open. Error: ", e.getMessage()); } } } // Turning On flash private void turnOnFlash() { if (!isFlashOn) { if (camera == null || params == null) { return; } // play sound playSound(); params = camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.startPreview(); isFlashOn = true; // changing button/switch image toggleButtonImage(); } } // Turning Off flash private void turnOffFlash() { if (isFlashOn) { if (camera == null || params == null) { return; } // play sound playSound(); params = camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(params); camera.stopPreview(); isFlashOn = false; // changing button/switch image toggleButtonImage(); } } // Playing sound // will play button toggle sound on flash on / off private void playSound(){ if(isFlashOn){ mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off); }else{ mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on); } mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub mp.release(); } }); mp.start(); } /* * Toggle switch button images * changing image states to on / off * */ private void toggleButtonImage(){ if(isFlashOn){ btnSwitch.setImageResource(R.drawable.btn_switch_on); }else{ btnSwitch.setImageResource(R.drawable.btn_switch_off); } } @Override protected void onDestroy() { super.onDestroy(); } @Override protected void onPause() { super.onPause(); // on pause turn off the flash turnOffFlash(); } @Override protected void onRestart() { super.onRestart(); } @Override protected void onResume() { super.onResume(); // on resume turn on the flash if(hasFlash) turnOnFlash(); } @Override protected void onStart() { super.onStart(); // on starting the app get the camera params getCamera(); } @Override protected void onStop() { super.onStop(); // on stop release the camera if (camera != null) { camera.release(); camera = null; } } }
Hi there! I am Founder at androidhive and programming enthusiast. My skills includes Android, iOS, PHP, Ruby on Rails and lot more. If you have any idea that you would want me to develop? Let's talk: [email protected]
How To Create Flashlight App Android
Source: https://www.androidhive.info/2013/04/android-developing-flashlight-application/
Posted by: batesountracentle.blogspot.com
0 Response to "How To Create Flashlight App Android"
Post a Comment