Customising ActionBar in Android App

To hide the action and status bar entirely from the activity, apply the following code within the <application>...</application> tags of your manifest file:

android:theme="@style/AppTheme.NoActionBar"

In styles.xml add the following:

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>

To customise the design of your action bar provide the following code in your activity file:

ActionBar actionbar = getSupportActionBar();

// Applies the custom action bar style
getSupportActionBar().setDisplayOptions
(actionbar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);

Then in your XML you need to create the desired action bar, for example in my project action_bar.xml layout looks as below:

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/action_bar_title"
android:text="Robots"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"/>

</LinearLayout>

Another thing you may want to alter is the action bar title and make it unique for each activity. In such a case you need to set the action bar title programmatically. You can do this like this:

// Changes the action bar title
TextView title = (TextView) getSupportActionBar().getCustomView()
.findViewById(R.id.action_bar_title);
title.setText(R.string.q2_name);

If you wish to see the full implementation you can check the app on Github.

Pretty simple few lines of code but this makes your app a way nicer. Don’t you think? :)