Set status bar color from DialogFragment

十年热恋 提交于 2019-12-03 03:15:36
Raz Tourgman

In order to setStatusBarColor you need to set the flag: FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS

"Flag indicating that this Window is responsible for drawing the background for the system bars." (Google API level 21)

Try this code:

public void setStatusBarColorIfPossible(int color) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
       getWindow().setStatusBarColor(color);
    }
}

use FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS and change hight child in index 0 in getDecorView.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       if (getDialog().getWindow() != null) {

              // Flag
              getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 

              // DeviceScreenHeight
              WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
              DisplayMetrics displayMetrics = new DisplayMetrics();
              windowManager.getDefaultDisplay().getMetrics(displayMetrics);
              int deviceScreenHeight = displayMetrics.heightPixels;

              // Change height child At index zero 
              ((ViewGroup) getDialog().getWindow().getDecorView().getRootView()).getChildAt(0).getLayoutParams().height = deviceScreenHeight;

        }
}

This works for me.

 @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Objects.requireNonNull(Objects.requireNonNull(getDialog()).getWindow()).addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        Objects.requireNonNull(Objects.requireNonNull(getDialog()).getWindow()).setStatusBarColor(getResources().getColor(R.color.light_green_400));
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!