Search code examples
androidandroid-fragmentsandroid-actionbarandroid-statusbar

Bar status not changing color and ActionBar good practices


I'm quite new in the world of android, and I'm not sure about how to modify properly the Action bar.

In my case, the main activity has a FrameLayout where I switch between fragments with a Navigation Drawer.

I've seen that there are several ways to change the actionbar parameters, but I'm not quite sure which one is the best. What I'm using so far to change the ActionBar (and the fragment) is the following:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void displayView(int position) {
        // update the main content by replacing fragment
        Fragment fragment = null;
        ImageView imageView = new ImageView(getSupportActionBar().getThemedContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        switch (position) {
            case 0:
                imageView.setImageResource(R.drawable.ic_launcher);
                getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.main_menu)));
                fragment = new Seccion1MainMenu();
                break;
            case 1:
                imageView.setImageResource(R.drawable.ic_security);
                getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.security_menu)));
                fragment = new Seccion2Security();
                break;
            case 2:
                imageView.setImageResource(R.drawable.ic_light);
                getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.light_menu)));
                fragment = new Seccion3();
                break;
            case 3:
                imageView.setImageResource(R.drawable.ic_fan);
                getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.fan_menu)));
                fragment = new Seccion4();
                break;

            default:
                imageView.setImageResource(R.drawable.ic_launcher);
                break;
        }
        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            getSupportActionBar().setCustomView(imageView);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("Alvaro", "MainActivity - Error cuando se creo el fragment");
        }

As you can see in the code, I change the color of the Action Bar, the title and the image. Is this a proper way to change modify the content or I'm forcing the machine?

What I can't manage to do is to change the color of the status bar autmatically. I've read several posts and I haven't been able to achieve it. One of the solutions proposed was to add this line:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

But I got no result. Do you have any idea? Thanks for your help :)


Solution

  • For status bar:

    this one work for me

    //changing statusbar
    if (android.os.Build.VERSION.SDK_INT >= 21){
                    Window window = this.getWindow();
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    window.setStatusBarColor(this.getResources().getColor(R.color.primary_dark));
                }
    

    hope it help