Flashlight Android App

A basic android application that allows the user to toggle a torch or torchlight by pressing a button should be created by all newcomers to the field of Android programming. One would therefore be able to create their own Android torch application with a straightforward layout after reading this guide.

In this article, you will learn how to create your Flashlight Example of Flashlight in Android. So let’s take example of Flashlight activity codes  in Android of Mobile application

Example of Flashlight

                                                                                            

Step-by-Step Implementation

 Step 1: Create a New Project in Android ​studio (File >new  project). 


Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

Implement the same in​voke the following code inside activity_main.xml file.

 <?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:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="HardcodedText">
<ImageView
android:id="@+id/imgonoff"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="228dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.554"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtonoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="Flashlight is turned OFF"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgonoff" />
</androidx.constraintlayout.widget.ConstraintLayout>
 Step 3: Create an MainActivity.kt file in your package folder .

Implement the same invoke the fol​low​ing code inside MainActivity.kt file.

 import android.annotation.SuppressLint
import android.content.Context
import android.hardware.camera2.CameraAccessException
import android.hardware.camera2.CameraManager
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
private var imgonoff: ImageView? = null
private var txtonoff: TextView? = null
private var isonoroff: Boolean? = null
private var cameraManager: CameraManager? = null
private var getCameraID: String? = null

@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
isonoroff = false
imgonoff = findViewById(R.id.imgonoff)
txtonoff = findViewById(R.id.txtonoff)
cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
imgonoff?.setBackgroundResource(R.drawable.off)
try {
getCameraID = cameraManager!!.cameraIdList[0]
} catch (e: CameraAccessException) {
e.printStackTrace()
}
imgonoff?.setOnClickListener {
if (isonoroff == true) {
try {
imgonoff?.setBackgroundResource(R.drawable.off)
cameraManager!!.setTorchMode(getCameraID!!, false)
Toast.makeText(
this@MainActivity,
"Flashlight is turned OFF",
Toast.LENGTH_SHORT
)
.show()
txtonoff?.setText("Flashlight is turned OFF")
} catch (e: CameraAccessException) {
e.printStackTrace()
}
isonoroff = false
} else {
try {
imgonoff?.setBackgroundResource(R.drawable.on)
cameraManager!!.setTorchMode(getCameraID!!, true)
txtonoff?.setText("Flashlight is turned ON")
Toast.makeText(this@MainActivity, "Flashlight is turned ON", Toast.LENGTH_SHORT)
.show()
} catch (e: CameraAccessException) {

e.printStackTrace()
}
isonoroff = true
}
}
}


override fun finish() {
super.finish()
try {
cameraManager!!.setTorchMode(getCameraID!!, false)

Toast.makeText(this@MainActivity, "Flashlight is turned OFF", Toast.LENGTH_SHORT).show()
} catch (e: CameraAccessException) {
e.printStackTrace()
}
}

}

Step 4 : Output of above example.

                                                                  

Happy coding!

365Bloggy July 19, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive