最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Android 15 Status bar color not changing in edge to edge - Stack Overflow

programmeradmin3浏览0评论

I am trying to implement android 15 (API 35) edge to edge UI, its look working except the status bar color i have with the navigation drawer(with toolbar) in android 13 or lower.I tried to follow official guide and blog from chris but no matter what i do, i am not able to change my status bar color green as required.

Android Guide

Blog

The method i am using.

private void edgeToedge() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
            EdgeToEdge.enable(this);

            WindowInsetsControllerCompat windowInsetsController =
                    ViewCompat.getWindowInsetsController(getWindow().getDecorView());
            if (windowInsetsController == null) {
                return;
            }

            windowInsetsController.setAppearanceLightNavigationBars(true);
            WindowCompat.getInsetsController(getWindow(),getWindow().getDecorView())
                    .setAppearanceLightStatusBars(true);




            WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
            ViewCompat.setOnApplyWindowInsetsListener(toolbar, new OnApplyWindowInsetsListener() {
                @NonNull
                @Override
                public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets) {
                    // Retrieve the insets for the system bars (status bar, nav bar, etc.)
                    Insets systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                    // Update layout params: add a top margin equal to the status bar height.
                    ViewGroup.LayoutParams lp = v.getLayoutParams();
                    if (lp instanceof ViewGroup.MarginLayoutParams) {
                        ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) lp;
                        marginParams.topMargin = systemBarsInsets.top;
                        v.setLayoutParams(marginParams);
                    }
                    // Alternatively, you can update padding if that better suits your design:
                    // v.setPadding(v.getPaddingLeft(), originalPaddingTop + systemBarsInsets.top,
                    //              v.getPaddingRight(), v.getPaddingBottom());

                    return insets;
                }
            });

        }

    }

I am trying to implement android 15 (API 35) edge to edge UI, its look working except the status bar color i have with the navigation drawer(with toolbar) in android 13 or lower.I tried to follow official guide and blog from chris but no matter what i do, i am not able to change my status bar color green as required.

Android Guide

Blog

The method i am using.

private void edgeToedge() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
            EdgeToEdge.enable(this);

            WindowInsetsControllerCompat windowInsetsController =
                    ViewCompat.getWindowInsetsController(getWindow().getDecorView());
            if (windowInsetsController == null) {
                return;
            }

            windowInsetsController.setAppearanceLightNavigationBars(true);
            WindowCompat.getInsetsController(getWindow(),getWindow().getDecorView())
                    .setAppearanceLightStatusBars(true);




            WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
            ViewCompat.setOnApplyWindowInsetsListener(toolbar, new OnApplyWindowInsetsListener() {
                @NonNull
                @Override
                public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets) {
                    // Retrieve the insets for the system bars (status bar, nav bar, etc.)
                    Insets systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                    // Update layout params: add a top margin equal to the status bar height.
                    ViewGroup.LayoutParams lp = v.getLayoutParams();
                    if (lp instanceof ViewGroup.MarginLayoutParams) {
                        ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) lp;
                        marginParams.topMargin = systemBarsInsets.top;
                        v.setLayoutParams(marginParams);
                    }
                    // Alternatively, you can update padding if that better suits your design:
                    // v.setPadding(v.getPaddingLeft(), originalPaddingTop + systemBarsInsets.top,
                    //              v.getPaddingRight(), v.getPaddingBottom());

                    return insets;
                }
            });

        }

    }

Share Improve this question asked Feb 7 at 13:43 RituRitu 6562 gold badges15 silver badges40 bronze badges 3
  • since your content is drawn behind the status bar with edge to edge you are not supposed to change the status bar color anymore, instead you are supposed to extend your ui into the status bar. There are countless questions about this on StackOverflow – tyczj Commented Feb 7 at 13:54
  • @tyczj as i am using navigation drawer so its not good idea to put my navigation and toolbar behind status bar as it will effects the ux. I don't see anything in official documentation about this fix, Is there any way to make this work ? – Ritu Commented Feb 7 at 14:01
  • 1 please look at this question stackoverflow.com/questions/79018063/… and this medium.com/androiddevelopers/… – tyczj Commented Feb 7 at 14:08
Add a comment  | 

1 Answer 1

Reset to default 0
private void edgeToedge() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    // Ensure EdgeToEdge is correctly implemented or remove if not needed.
    // EdgeToEdge.enable(this); // Uncomment if using a helper utility

    WindowInsetsControllerCompat windowInsetsController =
            ViewCompat.getWindowInsetsController(getWindow().getDecorView());
    if (windowInsetsController == null) {
        return;
    }

    // Set light status bar and light navigation bar appearance
    windowInsetsController.setAppearanceLightNavigationBars(true);
    windowInsetsController.setAppearanceLightStatusBars(true);

    // Allow drawing behind system bars
    WindowCompat.setDecorFitsSystemWindows(getWindow(), false);

    // Apply window insets for toolbar margin
    ViewCompat.setOnApplyWindowInsetsListener(toolbar, (v, insets) -> {
        Insets systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars());

        ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        marginParams.topMargin = systemBarsInsets.top; // Adjust for status bar height
        v.setLayoutParams(marginParams);

        return insets;
    });
}

}

发布评论

评论列表(0)

  1. 暂无评论