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
<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/back_wall"
tools:context="${packageName}.${activityClass}"
tools:ignore="HardcodedText" >
<Button
android:id="@+id/button1"
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="84dp"
android:background="@drawable/multi_button"
android:gravity="center_vertical|center_horizontal"
android:text="ON"
android:textColor="#ffffff"
android:textStyle="bold|normal" />
</RelativeLayout>
view raw layout.xml hosted with ❤ by GitHub

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".
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/circularbutton1" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/circularbutton2" />
<item android:drawable="@drawable/circularbutton1" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/abc_search_url_text_holo"/>
<stroke android:width="2sp" android:color="#00ff00" />
</shape>

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.
private void sound(){
if(flag==true)
myMediaPlayer= MediaPlayer.create(getApplicationContext(), R.raw.snd1);
else
myMediaPlayer= MediaPlayer.create(getApplicationContext(), R.raw.snd2);
//start the sound
myMediaPlayer.start();
//oncomplete listner
myMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
myMediaPlayer.release();
}
});
}
view raw sound.java hosted with ❤ by GitHub

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.

package com.example.flash_test;
import com.example.flash_test.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private Button flash;
private Boolean flag = true;
private Camera mCamera;
private MediaPlayer myMediaPlayer;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
getActionBar().hide();
flash= (Button)findViewById(R.id.button1);
flash.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if( getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) == false){
Toast.makeText(getApplicationContext(), "Flash not available", Toast.LENGTH_LONG).show();
finish();
}else{
if(flag == true){
//new DoSomethingOnBTNpress().execute("");
sound();
on();
get_camera();
}else{
//Toast.makeText(getApplicationContext(), "Flash off", Toast.LENGTH_LONG).show();
sound();
off();
release_cameara();
}
}
}
});
}
private void on(){
flag=false;
flash.setTextColor(Color.RED);
flash.setText("OFF");
flash.setBackgroundResource(R.drawable.multi_button_red);
}
private void off(){
flag=true;
flash.setTextColor(Color.WHITE);
flash.setText("ON");
flash.setBackgroundResource(R.drawable.multi_button);
}
private void get_camera(){
mCamera = Camera.open();
Camera.Parameters params = (android.hardware.Camera.Parameters) mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters((android.hardware.Camera.Parameters) params);
mCamera.startPreview();
}
private void release_cameara(){
mCamera.stopPreview();
mCamera.release();
}
private void sound(){
if(flag==true)
myMediaPlayer= MediaPlayer.create(getApplicationContext(), R.raw.snd1);
else
myMediaPlayer= MediaPlayer.create(getApplicationContext(), R.raw.snd2);
//start the sound
myMediaPlayer.start();
//oncomplete listner
myMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
myMediaPlayer.release();
}
});
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onPause();
if(mCamera!=null)
mCamera.release();
}
}

 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
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment