// Step 1: Import the necessary classes
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
class YourActivity : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Step 2: Set the content view for your activity
setContentView(R.layout.your_activity_layout)
// Step 3: Change the status bar color
changeStatusBarColor()
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun changeStatusBarColor() {
// Step 4: Get the window of the activity
val window: Window = window
// Step 5: Check if the Android version is Lollipop or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Step 6: Set the status bar color using the desired color resource
window.statusBarColor = ContextCompat.getColor(this, R.color.your_status_bar_color)
}
// Step 7: Make the navigation bar translucent (optional)
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
}