Set Material Toolbar height same as Action Title Bar height using XML

How to automatically determine and declare ToolBar height same as older ActionBar height.

By default the action title bar height is 72 pixels and some times its change for every device or android operating system version. After use of Toolbar as Action bar it show a little bit problem to all the developers that what is the Height they have to actually define which can exactly match as the Action Bar height. So with the use of android:layout_height=”?attr/actionBarSize” we can Set Material Toolbar height same as Action Title Bar height.

android-project-download-code-button

Follow all the below steps very carefully to add Toolbar Widget in your project.

1. Open your Project’s build.gradle ( Module : app ) and  add com.android.support:appcompat-v7:23.4.0  library file.

build-gradle-app

2. Here is the code to add appcompat library inside build.gradle file.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
}

Screenshot after add this code into build.gradle file.

support-appcompat

3. Open your project’s styles.xml file and change the theme with below theme.

<resources>

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

        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

    </style>

</resources>

4. Open colors.xml file and set primary color and secondary color for toolbar.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="ToolbarTitleTextColor">#F4FF81</color>
</resources>

How to Set Material Toolbar height same as Action Title Bar height using XML.

Code for MainActivity.java file.

package com.android_examples.toolbartitletextcolor_android_examplescom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

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

        toolbar = (Toolbar)findViewById(R.id.toolbar1);

        //Calling Color code from colors.xml file .
        toolbar.setTitleTextColor(getResources().getColor(R.color.ToolbarTitleTextColor));

        setSupportActionBar(toolbar);
    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:context="com.android_examples.toolbartitletextcolor_android_examplescom.MainActivity"
    android:background="#FFF3E0">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#009688" />

</RelativeLayout>

Screenshot

Set Material Toolbar height same as Action Title Bar height using XML

Click here to download Set Material Toolbar height same as Action Title Bar height using XML project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *