Flashlight Part I (Working with flash)

FlashLight Part I

(Working with Camera Flash and MediaPlayer sounds)


Hi everyone and welcome to another fun Android project at roadtodroid. This time we are going to make a Flash light app which is very easy and fun to make. I am going to complete this app in two parts, first with only the basic app with only flash and second with notifications and all other battery related stats. So stay tuned for part 2 and hope you like the post.


1. Create a new Android Application Project

  • Go to File > New > Android Application Project.
  • Give appropriate name for the project and move forward (I'm using API 19 for this project).
  • In the succeeding dialogs just click next and make sure to create an Empty Activity name MainActivity.java or of any name you want.
  • Finally click finish.

2. Design the layout of the App

  • Open res > layout > activity_main.xml.
  • Add one Button to it, as shown below.
  • Use the code provided to format the elements.
App layout

3. Design the Button Background

  • For designing our custom button we use a xml All xmls here are in res/drawable folder.
  • The selector defines three items which are for state_focused="true" ,state_focused="false"and default state.
  • Custom drawables are used to make circular button and with given width and stroke.
  • The examples are as below.
  • For more download the code and see the res > drawable-hdpi folder.
  • To add the xml with selector to our button background use android:background="@drawable/multi_buttom.xml".

4. Adding Sounds to the button

  • Add the sounds you like in the res > raw folder (if not there create a new one).
  • Then use the MediaPlayer class as shown in the code below.

5. Lets move to the coding part

  • In the Manifest file add the following permission-
  android:name="android.permission.CAMERA" />
  • In MainActivity.java in the onCreate method, bind the button to its layout counterpart using findViewById and set an OnClickListner for it.
  • In the OnClickListner, check for the availability of flash light on the device using getPackerManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH).
  • We create methods each for playing sound on button pressed , changing button layout, getting the camera and releasing camera.All these are as shown below.


 Stay tuned for Part 2



SHARE

Shobhit Chittora

Hi there, I am Shobhit Chittora. I am a college student and like to develop apps for Android platform.I started this blog to share my experiences with Android development and may be help you guys on the “roadtodroid”.

  • Image
  • Image