1

I am using a custom material toolbar (showing a back button and activity's title) in my activity. I would like the back button and title to be black in color.

This activity's navigation and status bar (battery, time) is currently immersive. I have tried overriding the default color using material tool bar method using the link below but its not working. The back button is still white in color. And I am not sure why is that so. I even play around to see if the tool bar is showing me open sans, but it does not neither does the changing the textSize work. How to change color of Toolbar back button in Android?

In my manifest file my activity is using the following theme.

<activity 
     android:name=".RegisterActivity"
     android:theme="@style/CustomActivityTheme">
</activity>

In my custom action_bar_layout.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:fontFamily="@font/open_sans"
        android:textSize="24sp"
        style="@style/Widget.MaterialComponents.Toolbar.Primary"
        android:theme="@style/MyThemeOverlay_Toolbar2"
        />
</LinearLayout>

In my color file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#00BEBA</color>
    <color name="colorPrimaryDark">#00BEBA</color>
    <color name="colorAccent">#F60606</color>
    <color name="appBackgroundColor">#00BEBA</color>
    <color name="textFieldColor">#000000</color>
</resources>

In my Style file

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/textFieldColor</item>
    </style>

 <style name="CustomActivityTheme" parent="AppTheme">
        <!-- Customize your theme here. -->
        <item name="windowNoTitle">false</item>
        <item name="windowActionBar">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
    </style>

    <style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
        <!-- color used by navigation icon and overflow icon -->
        <item name="colorOnPrimary">@color/textFieldColor</item>
        <item name="actionBarTheme">@style/MyThemeOverlay_Toolbar2</item>
        <item name="colorControlNormal">@color/colorAccent</item>
    </style>

    <style name="MyThemeOverlay_Toolbar2" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
        <!-- This attributes is used by title -->
        <item name="android:textColorPrimary">@color/textFieldColor</item>
        <!-- This attributes is used by navigation icon and overflow icon -->
        <item name="colorOnPrimary">@color/textFieldColor</item>
    </style>

In my registerActivity java code

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{

    Toolbar actionBar;
    int currentApiVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        Log.d("TestToolBar", "inside Register Activity");

        //using custom toolbar instead of custom textView as action bar
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.action_bar_layout);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        actionBar = findViewById(R.id.tvTitle);
        actionBar.setTitle(R.string.RegisterActionBar);

        hideNavigationBar();

    }

    public void hideNavigationBar() {
        currentApiVersion = android.os.Build.VERSION.SDK_INT;
        Log.d("TestToolBar", "api version is " + currentApiVersion);
        
        if (Build.VERSION.SDK_INT <= 21) {
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else {
            this.getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
                            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                            View.SYSTEM_UI_FLAG_FULLSCREEN);

            getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
            getWindow().setNavigationBarColor(getResources().getColor(R.color.colorPrimary));
        }
    }

tool bar image

UPDATED. After using setSupportActionBar(toolbar); It work. But I still need to amend my activity_register.xml. To include my action_bar_layout inside using include. If not my RegisterActivity.java cannot find my toolbar.(null exception error)

The font family can be changed by having a textview inside MaterialToolbar in action_bar_layout.xml

4nn4bel
  • 135
  • 2
  • 8

2 Answers2

1

As far as I know, there are simpler ways. In your Toolbar, add the following attribute:

app:titleTextAppearance="@style/ToolbarTheme"
app:navigationIcon="@drawable/ic_back"

Then in styles.xml, add

<style name="ToolbarThemeC" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="android:fontFamily">@font/lato</item>
    <item name="fontFamily">@font/lato</item>
    <item name="android:background">@color/White</item>
    <item name="android:textSize">24sp</item>
    <item name="iconTint">@color/colorPrimary</item>
    <item name="android:colorForeground">@color/colorPrimary</item>
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:textColorSecondary">@color/colorPrimary</item>
    <item name="actionOverflowButtonStyle">@style/OverFlowTheme</item>
    <item name="actionOverflowMenuStyle">@style/OverFlowMenuTheme</item>
</style>

And whatever other attributes you want.

Harsh Modani
  • 205
  • 1
  • 11
0

It's working for me.

<com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/toolbar"
        style="@style/MyToolbarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/teal_700"
        app:layout_constraintTop_toTopOf="parent"
        app:navigationIcon="@drawable/ic_back"
        app:navigationIconTint="@color/purple_700"
        app:title="Title" />

MyToolbarStyle:

<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
    <item name="titleTextColor">@color/teal_200</item>
    <item name="titleCentered">true</item>
    <item name="titleTextAppearance">@style/MyTextAppearanceToolbarStyle</item>
</style>

MyTextAppearanceToolbarStyle:

<style name="MyTextAppearanceToolbarStyle" parent="@style/TextAppearance.MaterialComponents.Headline6">
    <item name="android:fontFamily">@font/public_sans_italic</item>
    <item name="fontFamily">@font/public_sans_italic</item>
    <item name="android:textSize">30sp</item>
</style>

Result

enter image description here

LuzLaura98
  • 41
  • 6