Write a demo Android charging effect that happens on-screen when you plug your phone…
- User
Write a demo Android charging effect that happens on-screen when you plug your phone into the charger. Make it looking like Samsung's One UI
- Assistant
Write a demo Android charging effect that happens on-screen when you plug your phone into the charger. Make it looking like Samsung's One UI
Certainly! I'll guide you through creating a demo Android application that displays an on-screen charging effect when your phone is plugged into a charger, resembling the charging animation found in Samsung's One UI.
This demo will cover:
BroadcastReceiver to listen for charging events.Let's get started.
Open Android Studio and select "Create New Project".
Choose "Empty Activity" as the project template.
Configure your project:
ChargingEffectDemocom.example.chargingeffectdemoClick "Finish" to create the project.
We need to listen for changes in the device's charging state. We'll use a BroadcastReceiver to detect when the device is connected or disconnected from power.
Java: Create a new Java class named ChargingReceiver.java in your com.example.chargingeffectdemo package.
Kotlin: Create a new Kotlin class named ChargingReceiver.kt.
Java:
// ChargingReceiver.java
package com.example.chargingeffectdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ChargingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_POWER_CONNECTED.equals(action)) {
// Device is connected to power
Intent chargingIntent = new Intent(context, ChargingActivity.class);
chargingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chargingIntent);
}
}
}
Kotlin:
// ChargingReceiver.kt
package com.example.chargingeffectdemo
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
class ChargingReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (Intent.ACTION_POWER_CONNECTED == action) {
// Device is connected to power
val chargingIntent = Intent(context, ChargingActivity::class.java)
chargingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(chargingIntent)
}
}
}
Open your AndroidManifest.xml file and register the ChargingReceiver.
<!-- AndroidManifest.xml -->
<application
...>
...
<receiver android:name=".ChargingReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
...
</application>
Create an activity that will display the charging effect when the charger is connected.
Java:
// ChargingActivity.java
package com.example.chargingeffectdemo;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class ChargingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_charging);
}
}
Kotlin:
// ChargingActivity.kt
package com.example.chargingeffectdemo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class ChargingActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_charging)
}
}
Add the new activity to your AndroidManifest.xml.
<!-- AndroidManifest.xml -->
<application
...>
...
<activity android:name=".ChargingActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"/>
...
</application>
We'll create a layout that mimics Samsung's One UI charging effect.
Create a new XML layout file named activity_charging.xml in res/layout.
<!-- res/layout/activity_charging.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/charging_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<!-- Battery Outline Image -->
<ImageView
android:id="@+id/battery_outline"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_battery_outline"
android:contentDescription="@string/battery_outline" />
<!-- Battery Level Text -->
<TextView
android:id="@+id/battery_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/battery_outline"
android:layout_marginTop="20dp"
android:text="85%"
android:textColor="#FFFFFF"
android:textSize="48sp"
android:textStyle="bold" />
<!-- Charging Animation -->
<!-- You can use Lottie Animation or create a custom animation -->
</RelativeLayout>
Since we cannot use Samsung's assets, we'll create a simple battery outline icon.
Right-click on res/drawable folder, select "New" > "Vector Asset".
In the Asset Type, select "Clip Art".
Search for "battery" and select a suitable icon (e.g., battery_full).
Name it ic_battery_outline.xml.
Alternatively, you can download a free battery outline icon and add it to the drawable folder.
The TextView displays the current battery percentage.
We need to retrieve the current battery level and update the TextView.
Java:
// ChargingActivity.java
package com.example.chargingeffectdemo;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ChargingActivity extends AppCompatActivity {
private TextView batteryPercentage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_charging);
batteryPercentage = findViewById(R.id.battery_percentage);
// Get battery level and display
int level = getBatteryLevel();
batteryPercentage.setText(level + "%");
}
private int getBatteryLevel() {
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, intentFilter);
int level = 0;
int scale = 100;
if (batteryStatus != null) {
level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
}
return (int) ((level / (float) scale) * 100);
}
}
Kotlin:
// ChargingActivity.kt
package com.example.chargingeffectdemo
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class ChargingActivity : AppCompatActivity() {
private lateinit var batteryPercentage: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_charging)
batteryPercentage = findViewById(R.id.battery_percentage)
// Get battery level and display
val level = getBatteryLevel()
batteryPercentage.text = "$level%"
}
private fun getBatteryLevel(): Int {
val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
val batteryStatus = this.registerReceiver(null, intentFilter)
var level = 0
var scale = 100
batteryStatus?.let {
level = it.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
scale = it.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
}
return ((level / scale.toFloat()) * 100).toInt()
}
}
Update the activity to display in full-screen mode and remove the status bar.
Java & Kotlin:
Add the following code inside the onCreate method, before setContentView.
// Remove status bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Hide action bar
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
Kotlin:
// Remove status bar
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
// Hide action bar
supportActionBar?.hide()
Don't forget to import android.view.WindowManager.
To enrich the charging effect, we'll add an animation. You can use Lottie for Android to display beautiful animations.
Add the Lottie library to your build.gradle (Module: app) file.
dependencies {
...
implementation 'com.airbnb.android:lottie:5.0.3'
}
app/src/main/assets folder. Create the folder if it doesn't exist.Update your activity_charging.xml to include the animation.
<!-- res/layout/activity_charging.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lottie="http://schemas.android.com/tools"
...>
<!-- Existing views -->
<!-- Charging Animation -->
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/charging_animation"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_above="@id/battery_outline"
android:layout_marginBottom="20dp"
lottie:fileName="charging_animation.json"
lottie:autoPlay="true"
lottie:loop="true" />
</RelativeLayout>
No additional changes needed; the animation will start automatically.
To make the charging screen dismissible, we'll allow the user to tap anywhere to close it.
Java:
// ChargingActivity.java
@Override
public boolean onTouchEvent(MotionEvent event) {
// Close the activity on touch
finish();
return true;
}
Kotlin:
// ChargingActivity.kt
override fun onTouchEvent(event: MotionEvent?): Boolean {
// Close the activity on touch
finish()
return true
}
By default, activities cannot appear over the lock screen. To allow this:
Java & Kotlin:
In onCreate, add:
// Display over lock screen
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Be cautious with these flags, as they may affect user experience and battery life.
Automatically close the charging screen after a few seconds.
Java:
new Handler().postDelayed(() -> finish(), 5000); // Dismiss after 5 seconds
Kotlin:
Handler(Looper.getMainLooper()).postDelayed({ finish() }, 5000) // Dismiss after 5 seconds
Import android.os.Handler and android.os.Looper if necessary.
No special permissions are needed for the functionality we've implemented.
However, be aware that some devices may restrict apps from displaying over the lock screen due to battery optimization settings.
Run the app on your device.
Plug in the charger while the app is running.
Observe the charging animation appearing on the screen.
Unplug the charger or tap the screen to dismiss the charging screen.
You've now created a demo Android app that displays a custom charging effect when the device is plugged into a charger, inspired by Samsung's One UI. This demo includes:
If you have any questions or need further assistance with customization or additional features, feel free to ask!
Not yet included in a published release.