0

Hello I make one AppCompatActivity as a BaseActivity and in other all activity I extends with BaseActivity. Now in other classes I have to change tittle bar color and Tittle text.

This is my toolbar.xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_scrollFlags="scroll|enterAlways"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:title="@string/app_name" />

And setActionbar

@Override
public void setactionbar(int color) {
    // TODO Auto-generated method stub

    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
    getSupportActionBar().setElevation(0);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true); 

}

But not get success to change color and text

Thanks in Advance.

Nikhil
  • 3,711
  • 8
  • 32
  • 43
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49

2 Answers2

4

Try this..

 ActionBar actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#95CDBA")));
    actionBar.setTitle(Html.fromHtml("<font color='#000099'>Title bar</font>"));

or

  actionBar.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.yourcolor)));

and also add this if want enable title or disable

  actionBar.setDisplayShowTitleEnabled(true); //for enable or false for disable

For toolbar you need to like that

 toolbar.setTitleTextColor(0xFFFFFFFF);

or

toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.yourcolor);

For background

 toolbar.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.yourcolor)));
Arjun saini
  • 4,223
  • 3
  • 23
  • 51
0

Right way to do it is, First, Change you theme to...

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/your_title_bar_color</item> // default toolbar or actionbar color
    <item name="colorPrimaryDark">@color/your_title_bar_color_dark</item> //statusbar color
</style>

Now change your toolbar to,

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/abc_action_bar_default_height_material"
    android:orientation="vertical"
    android:background="@drawable/your_title_bar_color"
    android:title="@string/app_name"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

Now, in the class- make the method

private void setupToolbar() {
    setSupportActionBar(mToolbar);
}

All you need to do is call setupToolbar(). Happy coding.

Paresh P.
  • 6,677
  • 1
  • 14
  • 26