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));
}
}
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