0

i'm trying to center my a action bar text. I've linked the text to my styles.XML, and confirmed this works by changing the font color to red. But when i add android:gravity="center_horizontal", it doesnt seem to work.

My styles.xml code:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppThemeLight" 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>
</style>

<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">15sp</item>
    <item name="android:textStyle">italic</item>
    <item name="android:layout_gravity">center_horizontal</item>
</style>

My mainactivity oncreate event:

        setSupportActionBar(findViewById(R.id.tlb_roundedtoolbar))

My custom actionbar styling XML file:

   [<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fff" />
<corners android:radius="30dp" />
</shape>][1]

My mainactivity.XML

<androidx.appcompat.widget.Toolbar
    android:id="@+id/tlb_roundedtoolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/tlb_roundedtoolbar"
    android:elevation="4dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    app:titleTextColor="@android:color/holo_red_light"
    app:titleTextAppearance="@style/Toolbar.TitleText"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:layout_constraintTop_toTopOf="parent"
    />

Here's the result i'm getting with this code: [1]: https://i.stack.imgur.com/HgFc8.png

UPDATE

I've tried placing the toolbar within a linear layout, as suggested in this post: How to center align the ActionBar title in Android?.

However, my current code is still giving me the same result as before, with the text allinging to the left.

Current code (mainactivity.xml):

    <LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:orientation="vertical"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tlb_roundedtoolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/bcg_rounedrtoolbar"
        android:elevation="4dp"
        android:gravity="center"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:titleTextAppearance="@style/Toolbar.TitleText"
        app:titleTextColor="@color/colorPrimaryDark" />


</LinearLayout>

1 Answers1

0

Check this answer to see if it works for you. And if yes, investigate first to see if a solution to your problem has already been published. when you can't find it, ask and say what you tried https://stackoverflow.com/a/42465266/15351040

Chuy Porras
  • 125
  • 1
  • 6
  • The solution you've linked seems to be in Java, where i'm using kotlin. I've already tried to add android:gravity="center" and center_horizontal in the actionbar styling document and in my mainactivity.xml. I've tried searching for solutions, but all posts i've found reccomend the android:gravity fix, which doesnt seem to be working – YannickLorenzo Jul 19 '22 at 12:42
  • I've posted the code i've used in an update to my original post above – YannickLorenzo Jul 19 '22 at 13:10