FlashLight Android Source Code Example

Guys, welcome you all here. Here you can find the source code example of flashlight in android. Code units are below and its very simple to design it. Just learn it and try to implement your own awesome flashlight application and launch it in the google play with adding innovations in it. By developing an awesome fantabulous flashlight of paid version you can become a millionaire soon with the high amount of downloads that you get.

1. Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myflashever.android"
    android:versionCode="1"
    android:versionName="2.0" >

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name="com.myflashever.android.flashlight"
            android:screenOrientation="portrait" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2. Layout file - main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ToggleButton
        android:id="@+id/flashlight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textOn="Turn On"
        android:textOff="Turn Off"
        android:background="@drawable/change" />


</RelativeLayout>


2.1 @drawable/change
change.xml - for changing the ToggleButton image on click.


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/ooo"
          android:state_checked="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/lll"
        android:state_checked="false"/>
 </selector>


3. flashlight.java file

package com.myflashever.android;

import com.myflashever.android.R;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;

public class flashlight extends Activity {

private boolean isLightOn = false;

private Camera cam;

private ToggleButton button;

@Override
protected void onStop() {
super.onStop();

if (cam != null) {
cam.release();
}
}

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

button = (ToggleButton) findViewById(R.id.flashlight);

Context context = this;
PackageManager pm = context.getPackageManager();

if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return;
}

cam = Camera.open();
final Parameters p = cam.getParameters();

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

if (isLightOn) {

p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(p);
cam.stopPreview();
isLightOn = false;

} else {

p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
isLightOn = true;

}

}
});

}

}



Incredible FlashLight

Please find below a sample innovative free flashlight app in the market.
https://play.google.com/store/apps/details?id=com.myflashever.android&hl=en

No comments:

Post a Comment

My Profile