0% found this document useful (0 votes)
3 views4 pages

Flashlight Code

The document outlines the XML layout and Java code for an Android flashlight application. It includes UI components such as buttons, text views, and card views, as well as backend logic for managing flashlight functionality and battery status. The application allows users to control flashlight brightness and displays real-time battery information.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Flashlight Code

The document outlines the XML layout and Java code for an Android flashlight application. It includes UI components such as buttons, text views, and card views, as well as backend logic for managing flashlight functionality and battery status. The application allows users to control flashlight brightness and displays real-time battery information.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical No.

30 app:layout_constraintBottom_toBottomOf="@id/btnFlash"
app:layout_constraintStart_toStartOf="@id/btnFlash"
Frontend :- app:layout_constraintEnd_toEndOf="@id/btnFlash"
android:layout_marginTop="60dp"/>
<?xml version="1.0" encoding="utf-8"?> <!-- Status Text -->
<[Link] <TextView
xmlns:android="[Link] android:id="@+id/txtStatus"
xmlns:app="[Link] android:layout_width="wrap_content"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="match_parent" android:text="Flashlight is currently ON"
android:background="#0E0E0A" android:textColor="#F4E300"
android:padding="16dp"> android:textSize="16sp"
<!-- Title --> android:fontFamily="@font/poppins_medium"
<TextView app:layout_constraintTop_toBottomOf="@id/btnFlash"
android:id="@+id/txtTitle" app:layout_constraintStart_toStartOf="parent"
android:layout_width="wrap_content" app:layout_constraintEnd_toEndOf="parent"
android:layout_height="wrap_content" android:layout_marginTop="30dp"/>
android:text="Flashlight" <!-- Brightness Card -->
android:textColor="#FFFFFF" <[Link]
android:textSize="20sp" android:id="@+id/cardBrightness"
android:textStyle="bold" android:layout_width="0dp"
android:paddingTop="50dp" android:layout_height="wrap_content"
android:fontFamily="@font/poppins_medium" app:cardCornerRadius="20dp"
app:layout_constraintTop_toTopOf="parent" app:cardElevation="8dp"
app:layout_constraintStart_toStartOf="parent" app:cardBackgroundColor="#1C1C17"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/txtStatus"
android:layout_marginTop="24dp"/> app:layout_constraintStart_toStartOf="parent"
<!-- Big Power Button --> app:layout_constraintEnd_toEndOf="parent"
<ImageButton android:layout_marginTop="30dp"
android:id="@+id/btnFlash" android:layout_marginHorizontal="8dp">
android:layout_width="220dp" <LinearLayout
android:layout_height="220dp" android:layout_width="match_parent"
android:layout_marginTop="50dp" android:layout_height="wrap_content"
android:background="@drawable/bg_circle_yellow_glow" android:orientation="vertical"
android:src="@drawable/icon_power" android:padding="20dp">
android:scaleType="center" <!-- Brightness Row -->
android:contentDescription="Flash Button" <LinearLayout
app:layout_constraintTop_toBottomOf="@id/txtTitle" android:layout_width="match_parent"
app:layout_constraintBottom_toTopOf="@id/txtStatus" android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent" android:orientation="horizontal">
app:layout_constraintEnd_toEndOf="parent"/> <TextView
<!-- ON Text --> android:layout_width="wrap_content"
<TextView android:layout_height="wrap_content"
android:id="@+id/txtOn" android:fontFamily="@font/poppins_medium"
android:layout_width="wrap_content" android:text="BRIGHTNESS"
android:layout_height="wrap_content" android:textColor="#BFBFBF"
android:text="ON" android:paddingStart="15dp"
android:paddingTop="15dp" android:textSize="14sp" />
android:textColor="#000000" <TextView
android:textStyle="bold" android:id="@+id/txtPercent"
android:fontFamily="@font/poppins_medium" android:layout_width="wrap_content"
android:textSize="16sp" android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/btnFlash" android:layout_gravity="end"
android:layout_weight="1"
android:fontFamily="@font/poppins_medium" </LinearLayout>
android:gravity="end" </[Link]>
android:text="85%" <!-- Real Time Card -->
android:paddingEnd="15dp" <[Link]
android:textColor="#F4E300" android:id="@+id/cardTime"
android:textStyle="bold" /> android:layout_width="0dp"
</LinearLayout> android:layout_height="wrap_content"
<!-- SeekBar --> app:cardCornerRadius="20dp"
<SeekBar app:cardElevation="8dp"
android:id="@+id/seekBar" app:cardBackgroundColor="#1C1C17"
android:layout_width="match_parent" app:layout_constraintTop_toBottomOf="@id/cardBattery"
android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="10dp" app:layout_constraintEnd_toEndOf="parent"
android:max="5" android:layout_marginTop="20dp"
android:progress="4" android:layout_marginHorizontal="8dp">
android:progressTint="#F4E300" <LinearLayout
android:thumbTint="#F4E300" /> android:layout_width="match_parent"
</LinearLayout> android:layout_height="wrap_content"
</[Link]> android:orientation="horizontal"
<!-- Battery Status Card --> android:gravity="center_vertical"
<[Link] android:padding="20dp">
android:id="@+id/cardBattery" <!-- Clock Icon -->
android:layout_width="0dp" <ImageView
android:layout_height="wrap_content" android:layout_width="24dp"
app:cardCornerRadius="20dp" android:layout_height="24dp"
app:cardElevation="8dp" android:src="@drawable/icon_watch"/>
app:cardBackgroundColor="#1C1C17" <!-- Time Text -->
app:layout_constraintTop_toBottomOf="@id/cardBrightness" <TextView
app:layout_constraintStart_toStartOf="parent" android:id="@+id/txtTime"
app:layout_constraintEnd_toEndOf="parent" android:layout_width="wrap_content"
android:layout_marginTop="20dp" android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"> android:layout_marginStart="12dp"
<LinearLayout android:text="Time: 00:00:00"
android:layout_width="match_parent" android:textColor="#FFFFFF"
android:layout_height="wrap_content" android:textSize="14sp"
android:orientation="horizontal" android:fontFamily="@font/poppins_medium"/>
android:gravity="center_vertical" </LinearLayout>
android:padding="20dp"> </[Link]>
<!-- Battery Icon --> </[Link]>
<ImageView
android:layout_width="24dp" Backend :-
android:layout_height="24dp"
package [Link];
android:src="@drawable/ic_battery"/>
import [Link];
<!-- Battery Text -->
import [Link];
<TextView
import [Link];
android:id="@+id/txtBattery"
import [Link];
android:layout_width="wrap_content"
import [Link];
android:layout_height="wrap_content"
import [Link];
android:layout_marginStart="12dp"
import [Link];
android:text="Battery: 78% (Charging)"
import [Link];
android:textColor="#FFFFFF"
import [Link];
android:textSize="14sp"
import [Link];
android:fontFamily="@font/poppins_medium" />
import [Link];
import [Link]; [Link](v -> {
import [Link]; vibrateOnce();
import [Link]; if (!isFlashOn) {
import [Link]; turnOnFlash([Link]());
import [Link]; [Link]("OFF");
import [Link]; [Link]("Flashlight is currently ON");
import [Link]; isFlashOn = true;
import [Link]; } else {
import [Link]; turnOffFlash();
public class MainActivity extends AppCompatActivity { [Link]("ON");
// UI Components [Link]("Flashlight is currently OFF");
ImageButton btnFlash; isFlashOn = false;
SeekBar seekBar; }
TextView txtOn, txtPercent, txtStatus, txtBattery, txtTime, });
txtTitle; [Link](new
// ⚙ Logic [Link]() {
boolean isFlashOn = false; @Override
public void onProgressChanged(SeekBar seekBar, int
// Camera
level, boolean fromUser)
CameraManager cameraManager;
if (level < 1) level = 1;
String cameraId;
[Link]((level * 20) + "%");
BroadcastReceiver batteryReceiver;
if (isFlashOn) {
Vibrator vibrator;
turnOnFlash(level); }}
Handler timeHandler = new Handler();
@Override
Runnable timeRunnable;
public void onStartTrackingTouch(SeekBar seekBar)
@Override
{}
protected void onCreate(Bundle savedInstanceState) {
@Override
[Link](savedInstanceState);
public void onStopTrackingTouch(SeekBar seekBar)
setContentView([Link].activity_main);
{}
// Bind Views
});
btnFlash = findViewById([Link]);
batteryReceiver = new BroadcastReceiver() {
seekBar = findViewById([Link]);
@Override
txtOn = findViewById([Link]);
public void onReceive(Context context, Intent intent) {
txtPercent = findViewById([Link]);
int level =
txtStatus = findViewById([Link]);
[Link](BatteryManager.EXTRA_LEVEL, -1);
txtBattery = findViewById([Link]);
int scale =
txtTime = findViewById([Link]);
[Link](BatteryManager.EXTRA_SCALE, -1);
txtTitle = findViewById([Link]); // Bind int status =
txtTitle [Link](BatteryManager.EXTRA_STATUS, -1);
[Link](v -> {
Intent intent = new Intent([Link], int batteryPercent = (int) ((level / (float) scale) *
[Link]); 100);
startActivity(intent); String chargingStatus;
}); if (status ==
vibrator = (Vibrator) BatteryManager.BATTERY_STATUS_CHARGING) {
getSystemService(Context.VIBRATOR_SERVICE); chargingStatus = "Charging";
cameraManager = (CameraManager) } else if (status ==
getSystemService(Context.CAMERA_SERVICE); BatteryManager.BATTERY_STATUS_FULL) {
try { chargingStatus = "Full";
cameraId = [Link]()[0]; } else {
} catch (CameraAccessException e) { chargingStatus = "Not Charging";
[Link](); }
} [Link](
"Battery: " + batteryPercent + "% (" +
chargingStatus + ")" }
); } }; private void turnOffFlash() {
registerReceiver( try {
batteryReceiver, [Link](cameraId, false);
new } catch (CameraAccessException e) {
IntentFilter(Intent.ACTION_BATTERY_CHANGED) [Link]();
); }
timeRunnable = new Runnable() { }
@Override @Override
public void run() { protected void onDestroy() {
SimpleDateFormat sdf = [Link]();
new SimpleDateFormat("hh:mm:ss a",
[Link]()); if (batteryReceiver != null) {
String currentTime = [Link](new Date()); unregisterReceiver(batteryReceiver);
[Link]("Time: " + currentTime); }
[Link](this, 1000);
} if (timeHandler != null && timeRunnable != null) {
}; [Link](timeRunnable);
[Link](timeRunnable); }
} }
private void vibrateOnce() { }
if (vibrator == null || ![Link]()) return;
if ([Link].SDK_INT >=
Build.VERSION_CODES.O) {
[Link](
[Link](
60,
VibrationEffect.DEFAULT_AMPLITUDE
));
} else {
[Link](60);
}
}
private void turnOnFlash(int level) {
try {
if ([Link].SDK_INT >= 33) {

Method method = [Link](


"setTorchStrengthLevel",
[Link],
[Link]
);
[Link](cameraManager, cameraId, level);

} else {
[Link](cameraId, true);
}
} catch (Exception e) {
try {
[Link](cameraId, true);
} catch (CameraAccessException ex) {
[Link]();
}
}

You might also like