Turn on only camera FlashLight programmatically in android

How to enable-disable LED flash light using button click inside android app.

In this tutorial we are simply turn on and turn off android mobile phone device camera flash light. So here is the complete step by step tutorial for Turn on only camera FlashLight programmatically in android.

android-project-download-code-button

How to Turn on only camera FlashLight programmatically in android.

Please add CAMERA and android.hardware.camera permission to your AndroidManifest.xml file.

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

 

Code for MainActivity.java file.

 package com.turnononlycameraflashlight_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;

public class MainActivity extends Activity {

 Button BUTTONon,BUTTONoff;
 TextView textview;
 Camera camera;
 Parameters parameters;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview = (TextView)findViewById(R.id.textView1);
 BUTTONon = (Button)findViewById(R.id.button1);
 BUTTONoff = (Button)findViewById(R.id.button2);
 
 camera = Camera.open();
 parameters = camera.getParameters();
 
 BUTTONon.setOnClickListener(new View.OnClickListener() {
 
 @SuppressWarnings("deprecation")
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 textview.setText("FlashLight ON");
 parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
 camera.setParameters(parameters);
 camera.startPreview(); 
 
 }
 });
 
 
 BUTTONoff.setOnClickListener(new View.OnClickListener() {
 
 @SuppressWarnings("deprecation")
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 textview.setText("FlashLight OFF");
 parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
 camera.setParameters(parameters);
 camera.stopPreview();
 
 }
 });
 
 
 }
 }

Code for activity_main.xml layout file.

 <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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.turnononlycameraflashlight_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="FlashLight ON" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:text="FlashLight OFF" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="32dp"
 android:text="FlashLight OFF"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshots:

Turn on only camera FlashLight programmatically in android

 

flash-off

Click here to download Turn on only camera FlashLight programmatically in android project with source code.