How to change status bar color of AppCompatDialogFragment in Android 11 (API level 30)

540 Views Asked by At

I want to change the color of status bar to match the toolbar. I have try as many as possible code from other answers, and still not working. This code is working in Android 10, but not in Android 11. The code below provide me with a translucent status bar:


class SettingDialog : AppCompatDialogFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE)
    }

    override fun onResume() {
        super.onResume()
        dialog?.window?.apply {
            setLayout(MATCH_PARENT, MATCH_PARENT)
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) //This flag is deprecated
            statusBarColor = ContextCompat.getColor(context, R.color.primary_dark)
            val insetController = ViewCompat.getWindowInsetsController(decorView)
            insetController?.isAppearanceLightStatusBars = true
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val v = inflater.inflate(R.layout.dialog_setting, container, false)
        ...
        return v
    }

}

What i got: Status bar color has a translucent color

What i want: Status bar color has the same color as toolbar

1

There are 1 best solutions below

0
On

Looks like the problem was on the dim background. I've managed to get it working by removing the dim background.


class SettingDialog : AppCompatDialogFragment() {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.window?.apply {
            ...
            clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            ...
        }
    }
}