Thursday, 16 October 2025

How to turn ON the Torch!

 Do you wonder how can we lit the light of our Android Phone's Torch?

Here we have a very small code to do that!


We only need just one permission in our AndroidManifest.xml to turn ON the light:

<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />

So after adding this, the AndroidManifest.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Torch">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


Then in the activity_main.xml, create a button for user interaction:

<Button
android:id="@+id/torchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle Flashlight" />

Here is the full content of the activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">

<Button
android:id="@+id/torchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle Flashlight" />
</LinearLayout>


Finally in the MainActivity.java we have to do multiple thing:

1. Create object of CameraManager which is used to interact with the camera hardware, in this case, to control the flashlight.

2. Create a String object cameraId which holds the ID of the camera (typically used to refer to the back camera here).

3. Create a boolean isTorchOn to track whether the torch is currently on or off.

private CameraManager cameraManager;
private String cameraId;
private boolean isTorchOn = false;

4. The getSystemService(CAMERA_SERVICE) call retrieves a system-level service to access the camera hardware.

cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);

5. Add a try catch block:

  • This block attempts to get a list of camera IDs using cameraManager.getCameraIdList(). The first item in the list ([0]) corresponds to the back camera.

  • If there’s an error in accessing the camera, it catches the CameraAccessException and prints the stack trace.

try {
cameraId = cameraManager.getCameraIdList()[0]; // Use the back camera
} catch (CameraAccessException e) {
e.printStackTrace();
}

6. Set up the Button for Toggling Torch:

A Button named torchButton is found using findViewById(R.id.torchButton), which references the button element defined in the XML layout file.

The setOnClickListener method attaches a click listener to the button, so when it’s clicked, the toggleTorch() method will be called.

Button torchButton = findViewById(R.id.torchButton);
torchButton.setOnClickListener(view -> toggleTorch());


7. toggleTorch() Method: This method toggles the flashlight (torch) on and off.

It checks the isTorchOn flag:

If the torch is on (isTorchOn is true), it calls cameraManager.setTorchMode(cameraId, false) to turn the flashlight off.

If the torch is off (isTorchOn is false), it calls cameraManager.setTorchMode(cameraId, true) to turn it on.

After toggling the torch, it updates the isTorchOn flag accordingly.

private void toggleTorch() {
try {
if (isTorchOn) {
// Turn off the torch
cameraManager.setTorchMode(cameraId, false);
isTorchOn = false;
} else {
// Turn on the torch
cameraManager.setTorchMode(cameraId, true);
isTorchOn = true;
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}


So the complete code of MainActivity.java will be:

package com.prashantsj.apps.torch;

import android.app.Activity;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends Activity {

private CameraManager cameraManager;
private String cameraId;
private boolean isTorchOn = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize the CameraManager
cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);

// Get the camera ID (for the back camera)
try {
cameraId = cameraManager.getCameraIdList()[0]; // Use the back camera
} catch (CameraAccessException e) {
e.printStackTrace();
}

// Button to toggle torch
Button torchButton = findViewById(R.id.torchButton);
torchButton.setOnClickListener(view -> toggleTorch());
}

private void toggleTorch() {
try {
if (isTorchOn) {
// Turn off the torch
cameraManager.setTorchMode(cameraId, false);
isTorchOn = false;
} else {
// Turn on the torch
cameraManager.setTorchMode(cameraId, true);
isTorchOn = true;
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}



Monday, 29 September 2025

How to Update a Number Every Second (Latest Android OS)

We will achieve this automatic number update in Android.  Only two files to edit: xml and Java.



First we will set an id, change the text and increase the text size in our default 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/numberTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textSize
="50sp"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Now do some changes in Java file:

1. Create an object of TextView to set our updated text referencing from the xml file.

TextView numberTextView;

2. Create an int variable to hold the updated number. If you don't assign 0, it will set 0 by default.

int number = 0;

3. Create an object of Handler which helps run code with a delay or repeatedly on the main UI thread..

Handler handler = new Handler();

4. Create an object of Runnable.
A Runnable is like a task that you tell Android to run later — either once or repeatedly. In our example, we use it to update a number every second.

Runnable runnable = new Runnable() {
@Override
public void run() {
number++;
numberTextView.setText(String.valueOf(number));
handler.postDelayed(this, 1000);
}
};

Inside the run method we do three things:

a) Increase the value of number
b) Set the value to our text view 
c) Run again after one second

handler.postDelayed(this, 1000);

This line starts the first run after 1 second. After that, the runnable calls itself again every second — so the number keeps increasing.

4. Now inside onCreate method we have to write just two lines of code:

numberTextView = findViewById(R.id.numberTV);
handler.postDelayed(runnable, 1000);

Here we are referencing the text view from our xml file and again calling the postDelayed method of Handler object. But this is called only once when the app starts.

5. At last we have to get out of this continuous execution when the app is not in use, so just call the onDestroy method and call the removeCallbacks method of the handler object.

@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}


Here is the complete code:

package com.prashantsj.apps.autoupdatenumber;

import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

TextView numberTextView;
int number = 0;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
number++;
numberTextView.setText(String.valueOf(number));
handler.postDelayed(this, 1000);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);

numberTextView = findViewById(R.id.numberTV);
handler.postDelayed(runnable, 1000);
}

@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}
}





Thursday, 18 September 2025

How to do Wireless Debugging: The Latest Simple Way!

 To do quick debugging without the traditional way of connecting cable, there is a new and simple way.

Just follow these simple steps:

Step 1: First make sure that your phone & the laptop / pc are connected to the same WIFI network. 

Step 2: In your phone go to settings and select the About phone option.



Step 3: Tap the Build Number continuously 7 times so that you see a message which says 'You are now a Developer'.


Step 4: Now To find that 'Developer Option' you just enabled, just go to Settings and select the 'System' option.


 Step 5: After selecting System, scroll down to see '{} Developer Options'. Select it.


Step 6: After selecting it, you will see an option 'Use Developer options'. Just enable it.


Step 7: Scroll down to find USB debugging. Enable this too.

Step 8: Again scroll down to find Wireless debugging and enable it.


Step 9: Select the Wireless debugging so that you will find two options for pairing: with QR code or with pairing code.

Step 10: Let's select the 'Pair device with QR code' but before that you have to first select the phone from the device list.


Step 11: Once you select your device, a QR code will be displayed which you only have to scan and you are ready to do wireless USB debugging.







Sunday, 14 September 2025

How to create a cool app: 'Just Tap to Change Color!'

Here we will learn the easiest and shortest way to change color of a simple app with just a tap.

     

XML layout:

In the TextView, change the text 'Hello World!' to 'Just Tap', increase its size and make it bold:

android:text="JUST TAP!"
android:textSize="45sp"
android:textStyle="bold"
android:textColor="#ffff00"

Now our activity_main.xml has only this much code. We already have the id set to the Constraint Layout which is android:id="@+id/main"

<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JUST TAP!"
android:textSize="45sp"
android:textStyle="bold"
android:textColor="#ffff00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Now in the MainActivity.java, use the above mentioned id to set a new color:

private ConstraintLayout mainLayout;

Create an array of various colors that we want to appear randomly when we touch the screen:

private int colors[] = {Color.BLUE, Color.BLACK, Color.CYAN, Color.DKGRAY, Color.WHITE, Color.GREEN,
Color.YELLOW, Color.TRANSPARENT, Color.MAGENTA, Color.RED, Color.LTGRAY, Color.GRAY};

Create a Random object to get a random color:

private Random random = new Random();

In the OnCreate method, assign the value to mainLayout:

mainLayout = findViewById(R.id.main);

Finally override the OnUserInteraction method and change the background in this way:

mainLayout.setBackgroundColor(colors[random.nextInt(colors.length - 1)]);

So the full and complete code of MainActivity.java will be:

package com.prashantsj.apps.colorchanger;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

private ConstraintLayout mainLayout;
private int colors[] = {Color.BLUE, Color.BLACK, Color.CYAN, Color.DKGRAY, Color.WHITE, Color.GREEN,
Color.YELLOW, Color.TRANSPARENT, Color.MAGENTA, Color.RED, Color.LTGRAY, Color.GRAY};
private Random random = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
mainLayout = findViewById(R.id.main);
}

@Override
public void onUserInteraction() {
super.onUserInteraction();

mainLayout.setBackgroundColor(colors[random.nextInt(colors.length - 1)]);
}
}




Monday, 28 July 2025

How to Blink a Text

 Blinking a text in Android is very simple and needs very less code. Let's see how to make it!

 


In the activity_main.xml file you have to edit the android:text and add just these two lines under TextView tag:

android:id="@+id/textView"
android:textSize="40sp"
android:text="I AM BLINKING!"

So after making the changes, the activity_main.xml file will have these things:

<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="40sp"
android:text="I AM BLINKING!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


Now inside the MainActivity.java, below the setContentView method, just paste this code and you are done:

TextView blinkingText = findViewById(R.id.textView);
Animation blinkAnimation = new AlphaAnimation(0.0f, 1.0f);
blinkAnimation.setDuration(1000); // duration
blinkAnimation.setStartOffset(20);
blinkAnimation.setRepeatMode(Animation.REVERSE);
blinkAnimation.setRepeatCount(Animation.INFINITE);

Understand the code:

TextView blinkingText = findViewById(R.id.textView);

→ Retrieves the TextView with ID textView from the layout and stores it in blinkingText.


Animation blinkAnimation = new AlphaAnimation(0.0f, 1.0f);

→ Creates an alpha animation that changes the opacity from fully transparent to fully opaque.


blinkAnimation.setDuration(1000);

→ Sets the duration of one animation cycle to 1000 milliseconds (1 second).


blinkAnimation.setStartOffset(20);

→ Adds a 20-millisecond delay before the animation starts.


blinkAnimation.setRepeatMode(Animation.REVERSE);

→ Makes the animation reverse back to the start after reaching the end.


blinkAnimation.setRepeatCount(Animation.INFINITE);

→ Repeats the animation endlessly.


Finally the MainActivity.java will have this complete code:

package com.prashantsj.apps.blink;

import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);

TextView blinkingText = findViewById(R.id.textView);
Animation blinkAnimation = new AlphaAnimation(0.0f, 1.0f);
blinkAnimation.setDuration(1000); // duration
blinkAnimation.setStartOffset(20);
blinkAnimation.setRepeatMode(Animation.REVERSE);
blinkAnimation.setRepeatCount(Animation.INFINITE);

blinkingText.startAnimation(blinkAnimation);

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}


Wednesday, 16 July 2025

How to download and install the Android Studio Narwhal

 We know that Android Studio has launched its new version which is Narwhal. So let's download it step by step.



1. At first type "download android studio" in Google search and hit Enter.


2. Then in the search result, select the first one as shown in the image.



3. This will take you to the official website of Android Studio. Here click on the "Download Android Studio Narwhal" button. It will show a pop-up of Terms & Conditions.



4. Read and tick the checkbox which says "I have read and agree with the above terms and conditions". Finally click the Download Android Studio for Windows button. 



5. Save the .exe file where ever you want. It will take time to download as per you internet speed.



6. Once the download is over, open the .exe file. You can open it directly by clicking on the Chrome's download history.



7. Now you will see a installer window. Just keep selecting the Next option every time which is the Recommended setting by Android.



8. You can set the path that you want by clicking the Browse option. If you are fine with the default one, then hit Next.



9. The progressive bar shows that the installation has started and once it will over, it will stop.

 



10. Now click the Next option again to complete the process.



11. Click the Finish button to finish the total installation. Selecting the checkbox Start Android Studio will start the Android Studio right away.



12. Congratulations! Android Studio installed and it is opening!





ElevenLabs For Audio Models