How to Make Flashlight App in Android Studio
Here are the flashlight android app codeĀ
Step 1
Create a new project in Android Studio and name it “FlashlightApp”.
Open the activity_main.xml file located in the res/layout directory. Replace the existing code with the following XML layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/flashlightImage" android:layout_width="200dp" android:layout_height="200dp" android:src="@drawable/ic_flashlight_off" /> <Button android:id="@+id/toggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Toggle Flashlight" android:textColor="#FFFFFF" android:background="#FF4081" android:padding="8dp" android:layout_marginTop="24dp" android:onClick="toggleFlashlight" /> </LinearLayout> </RelativeLayout> |
Step 2
Open the MainActivity.java file located in the java/<your_package_name> directory. Replace the existing code with the following Java code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.example.flashlightapp; import android.content.Context; import android.content.pm.PackageManager; import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraManager; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private CameraManager cameraManager; private String cameraId; private boolean isFlashlightOn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { cameraId = cameraManager.getCameraIdList()[0]; } catch (CameraAccessException e) { e.printStackTrace(); } Button toggleButton = findViewById(R.id.toggleButton); toggleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { toggleFlashlight(v); } }); } public void toggleFlashlight(View view) { try { if (!isFlashlightOn) { cameraManager.setTorchMode(cameraId, true); isFlashlightOn = true; ((ImageView) findViewById(R.id.flashlightImage)).setImageResource(R.drawable.ic_flashlight_on); } else { cameraManager.setTorchMode(cameraId, false); isFlashlightOn = false; ((ImageView) findViewById(R.id.flashlightImage)).setImageResource(R.drawable.ic_flashlight_off); } } catch (CameraAccessException e) { e.printStackTrace(); } } } |
Step 3 Open the AndroidManifest.xml file located in the app/src/main directory. Replace the existing code with the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.flashlightapp"> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Step 4 Open the build.gradle (Module: app) file located in the root directory of the project. Ensure that the minSdkVersion is set to 21 or higher:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
apply plugin: 'com.android.application' android { compileSdkVersion 30 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.example.flashlightapp" minSdkVersion 21 targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility = 1.8 targetCompatibility = 1.8 } lintOptions { checkReleaseBuilds false abortOnError false } } dependencies { implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' } |
That’s it! You can now build and run the app on an Android device or emulator, and it will function as a flashlight.
That’s the complete code for creating a flashlight app in Android Studio using Java. However, if you’re looking to enhance the app further, here are a few suggestions:
1. Add a screen color feature: Allow users to change the screen color to different options like white, red, blue, etc., in addition to controlling the flashlight.
2. Implement a strobe light effect: Enable a strobe light mode that flashes the flashlight at a specific interval, creating a strobe light effect.
3. Add brightness control: Allow users to adjust the brightness level of the flashlight by controlling the intensity of the camera flash.
4. Include a widget: Create a widget that users can add to their device’s home screen for quick access to the flashlight functionality.
5. Support different screen orientations: Modify the app to handle screen orientation changes, ensuring a smooth user experience when rotating the device.
These are just a few ideas to extend the functionality of your flashlight app. You can explore additional features and improvements based on your requirements and creativity.
Hi, this is a comment.
To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
Commenter avatars come from Gravatar.