Hey everyone, this is a tutorial for the newbies, the people just started getting there hands dirty on android coding. I thought I would post some small apps tutorial for you all, so that you can enjoy the learning. Today, in this post we will learn how to Turn on Flashlight in Android using Code. As the title says “Turn on Flashlight Android – Building a Torch App”, so we are going to build a simple torch application today.

Torch Application Android Project
The first thing, as always we need our new project. So open Android Studio and create a new project with an Empty Activity.

Make sure you select API 23, as the minimum API. This is because the API that we are going to use is available on API 23 and later only, and the older one is deprecated. So to make things clean and easy to understand we are targeting only API 23 and later.
Creating Interface
Here you can play with the design, you can put nice images and you can make your screen look awesome. But the fun part is I am not going to bother about design, and yes I will create probably the worse UI. haha .
So come inside activity_main.xml and write the following code.

activity_main.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version=“1.0” encoding=“utf-8”?>
<android.support.constraint.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”
    tools:context=“.MainActivity”>
    <ToggleButton
        android:id=“@+id/toggleButton”
        android:layout_width=“wrap_content”
        android:layout_height=“wrap_content”
        android:text=“ToggleButton”
        app:layout_constraintBottom_toBottomOf=“parent”
        app:layout_constraintEnd_toEndOf=“parent”
        app:layout_constraintStart_toStartOf=“parent”
        app:layout_constraintTop_toTopOf=“parent” />
</android.support.constraint.ConstraintLayout>

The above XML will give you the following output. We only have a ToggleButton that we will use to turn on or off the android flash light.

  • So here we have one of the worse UI design of all time. ? LOL. Design if you want, or lets dive into adding the functionality.

Adding Feature in Manifest

  • As we are going to build a Torch application, and basically we want to programmatically turn on the flashlight of our android device. To do this we need to access this feature of android.
  • So come inside AndroidManifest.xml and add the line shown below.

AndroidManifest.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version=“1.0” encoding=“utf-8”?>
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”
    package=“net.simplifiedcoding.mytorch”>
    <! add this line that means our application will be using the device flash >
    <usesfeature android:name=“android.hardware.camera.flash” />
    <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”>
            <intentfilter>
                <action android:name=“android.intent.action.MAIN” />
                <category android:name=“android.intent.category.LAUNCHER” />
            </intentfilter>
        </activity>
    </application>
</manifest>

Building Basic Code Structure

  • We don’t have much to do, we only have a ToggleButton where we need to attach a listener to listen for the on and off events. To do this we will write the following code.
public class MainActivity extends AppCompatActivity {
    private ToggleButton toggleButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toggleButton = findViewById(R.id.toggleButton);
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               //called when the button status is changed          
            }
        });
    }

Checking if Flash is Available

  • Now let’s write some java code. When our application will enter the onCreate()method, first we will check if the device has a flash light or not. If the device do not have flashlight we will quit the application saying the flash is not available.
  • So our onCreate() method will be like this.

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Override
protectedvoidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
booleanisFlashAvailable=getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(!isFlashAvailable){
showNoFlashError();
}
toggleButton=findViewById(R.id.toggleButton);
toggleButton.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
@Override
publicvoidonCheckedChanged(CompoundButton buttonView,booleanisChecked){
}
});
}

Vivek Singh

Vivek is a blogger, builder of things by passion and handles the situation like pro. You’ll usually find him talking about technology, music, travel, automobiles and the game of Life, when not busy writing or speeding his way through the country!

Leave a Reply

Your email address will not be published. Required fields are marked *