In this tutorial, we will learn about Creating a Flashlight app using the kotlin language. This application is designed to turn flashlight on or off of the device with a simple click. Please note that this application is only available for mobile devices. I don’t know what it looks like on a tablet. equipment.
Content:
- Preview
- File: AndroidMenifest.xml
- File: MainActivity.kt
- File: activity_main.xml
Preview: Simple Flashlight app in Android
File: AndroidManifest.xml
Learn Java Programming & get Certificate
In the manifest file, add permissions.
To use the Camera and FlashLight, you must add Permissions to the Android Manifest.xml file. To do so, open your AndroidManifest.xml file and paste the code given below to your AndroidManifest.xml file.
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" />
–>The code given below is the AndroidManifest.xml file code.
<?xml version="1.0" encoding="utf-8"?> <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/Theme.FlashlightApp"> <activity android:name=".MainActivity" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
File: MainActivity.kt
–> In MainActivity.kt file, we will implement the backend of the flashlight application in the kotlin language, implement functions to get access to the camera service.
package com.example.flashlightapp import android.content.Context import android.hardware.camera2.CameraManager import android.os.Build import android.os.Bundle import android.view.View import android.widget.ImageButton import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity @Suppress("DEPRECATION") class MainActivity : AppCompatActivity() { private lateinit var cameraM: CameraManager private lateinit var powerBtn: ImageButton var isFlash = false @RequiresApi(Build.VERSION_CODES.LOLLIPOP) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) powerBtn = findViewById(R.id.on) cameraM = getSystemService(Context.CAMERA_SERVICE) as CameraManager powerBtn.setOnClickListener { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { flashLightOnRoOff(it) } } } @RequiresApi(Build.VERSION_CODES.M) private fun flashLightOnRoOff(v: View?) { /**set flash code*/ if (!isFlash) { val cameraListId = cameraM.cameraIdList[0] cameraM.setTorchMode(cameraListId, true) isFlash = true powerBtn.setImageResource(R.drawable.on) } else { val cameraListId = cameraM.cameraIdList[0] cameraM.setTorchMode(cameraListId, false) isFlash = false powerBtn.setImageResource(R.drawable.off) } } }
File: activity_main.xml
–> activity_main.xml file contains two textview and an Imagebutton which has on or off functionality.
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" android:background="@drawable/bg" tools:context=".MainActivity"> <TextView android:id="@+id/flashlight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="199dp" android:layout_marginBottom="62dp" android:fontFamily="sans-serif-smallcaps" android:text="@string/flashlight" android:textColor="@color/teal_200" android:textSize="30sp" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/on" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.496" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <ImageButton android:id="@+id/on" android:layout_width="130dp" android:layout_height="130dp" android:background="@null" android:scaleType="centerCrop" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.648" app:srcCompat="@drawable/on" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="14dp" android:text=" DEVELOPED BY - STAR COMMUNITY" android:textColor="#a4a4a4" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
- We hope that this guide will assist you in quickly creating a flashlight Application for Android. If you have any problems, please post them in the comments section and we will gladly assist you.
Pingback: Multiplication Table Application Tutorial in Android - Developers Dome
Pingback: Creating Transparent Activity in Android - Developers Dome
Pingback: Webview Example Tutorial in Android Studio - Developers Dome
Pingback: Option Menu in Android With Example - Developers Dome
Pingback: Calculator App with Example in Android - Developers Dome
Pingback: Media Player in Android | Tutorial - Developers Dome