Search code examples
androidandroid-layoutandroid-toolbar

Issues in customizing toolbar in Android


I wanted to achieve the following task

  • Remove the default toolbar
  • Build a custom toolbar
  • change the default title, title text color, title text alignment in the toolbar

I successfully removed the default toolbar but while building the custom toolbar I'm not able to

  • Change the title text color
  • Change the titleText alignment

I tried out the existing solutions provided to the above problems at stack overflow but nothing seemed to be helpful in my case

Here's toolBar.xml file

enter code here
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolbarC"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"

android:theme="@style/Mystyle"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#ffffff">


</android.support.v7.widget.toolbar>

Here is my main xml file

enter code here`
<?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="match_parent"
xmlns:layout="http://schemas.android.com/apk/res-auto"
android:background="#3dcc24">

<include
layout="@layout/clubs_tool_bar" />

</LinearLayout>

Here's is my styles.xml file

<resources>

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

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" 
parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="Mystyle" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name = "android:textSize">30sp</item>



</style>
</resources>

enter image description here

This is what my screen looks like


Solution

  • You need to address the appropriate class so use android.support.v7.widget.Toolbar

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/toolbarC"
    
        app:title="YourTitle"
    
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"    
        android:theme="@style/Mystyle"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextColor="#ffffff"/>
    

    instead of

    android.support.v7.widget.toolbar