Builduing a Simple Flashlight App in Android Studio

 In this post, you will learn how to create a simple flashlight app to enable or disable the phone's flashlight in android stuido using Java.

    For building the app, first, create a new project and add the power icon from vector assets in your drawable folder. Now, create two new drawable resource files that will be the background for our switch on and switch off button. Add the following code to the files:

Code for switch on button:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/holo_blue_bright"/>
<corners android:radius="100dp"/>
</shape>
view raw xml hosted with ❤ by GitHub

Code for switch off button:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/darker_gray"/>
<corners android:radius="100dp"/>
</shape>
view raw xml hosted with ❤ by GitHub

Once this is done, in the activity_main.xml file, create the layout for the app screen. Add the following code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#4527A0">
<ImageButton
android:id="@+id/switch_on"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="@drawable/green_round"
android:src="@drawable/ic_power" />
<ImageButton
android:id="@+id/switch_off"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_power"
android:layout_centerInParent="true"
android:background="@drawable/gray_round"/>
</RelativeLayout>
view raw xml hosted with ❤ by GitHub

The background of the image buttons are the switch_on and switch_off drawable files that we created earlier.

Now, in the MainActivity.java class , add the following code:

public class MainActivity extends AppCompatActivity {
ImageButton switch_on,switch_off;
Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switch_on = findViewById(R.id.switch_on);
switch_off = findViewById(R.id.switch_off);
camera = Camera.open();
final Camera.Parameters parameters = camera.getParameters();
switch_on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch_off.setVisibility(View.VISIBLE);
switch_on.setVisibility(View.GONE);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
}
});
switch_off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch_off.setVisibility(View.GONE);
switch_on.setVisibility(View.VISIBLE);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
}
});
}
}
view raw java hosted with ❤ by GitHub

Here the Camera used is Camera (android.hardware) and not the graphics one.


Now finally, in the AndroidManifest.xml file, add the permissions to allow access to the device's camera.

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

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

And that's it. You have successfully built your flashlight app...

Output:

Flashlight OFF

Flashlight ON






Comments

Popular posts from this blog

Custom SeekBar in Android Studio .

How to play Audio on Button Click in app | Android App Development | Java

How to implement simple Pie Chart in app | Android Studio | Java | Android App Development