andrDev
andrDev

Reputation: 23

Navigation Drawer: Action Bar color change

I have an activity class that has a Navigation drawer. i want to change the action bar color when i go from one fragment to another from the navigation drawer. How do i do this.

My code is similar to this:http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

Plz Help.

Upvotes: 0

Views: 3291

Answers (3)

ali zarei
ali zarei

Reputation: 1241

try this

<item name="colorControlNormal">@color/colorControlNormal</item>

worked for me

Upvotes: 0

Kira Nofans
Kira Nofans

Reputation: 186

Same as mine, I got a navigation drawer too. so find the AppBarLayout in app_bar_main.xml layout

  <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff5d7"
    android:theme="@style/AppTheme.AppBarOverlay">

and you can change its background. I hope this may help

Upvotes: 1

Kostas Drak
Kostas Drak

Reputation: 3260

You can do it with the android.support.v7.widget.Toolbar. Create a layout file like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#cc00ff"
    android:layout_height="?android:attr/actionBarSize" />

To include the Toolbar layout in your layout do it like

<include android:id="@+id/toolBar" layout="@layout/custom_toolbar" />

Then for each fragment that you return in the displayView() method you can modify it like this:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        ((ActionBarActivity) getActivity()).getSupportActionBar().setColor(Color.GREEN);
        super.onActivityCreated(savedInstanceState);

    }

Then your HostActivity which is MainActivity you can setup the Toolbar like:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar tb = (Toolbar)findViewById(R.id.toolBar); 
        setSupportActionBar(tb);
        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));


        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // enabling action bar app icon and behaving it as toggle button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }
    }

Make your MainActivity extend ActionBarActivity instead of Activity and in your styles.xml modify your theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>

Hope it helps!!!

Upvotes: 0

Related Questions