2

To start my objective is to center my title. I have started a project by selecting the bottom navigation activity template. This came with 3 fragments and a main activity. I am attempting to center the title of the top action bar. I have attempted to access the mobile_navigation by id and align the text within the MainActivity but this does not seem to work. I also tried adding a textAlignment attribute in the homeFragment.xml but this did not work either. I do not find any similar scenarios anyone know how could I be able to resolve this?

mobile_navigation.xml:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.Example.exmaple.ui.home.HomeFragment"
        android:label="@string/app_name"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.Example.exmaple.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.Example.exmaple.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />
</navigation>

I also attempted to add:

android:gravity="center"

Any feedback welcomed.

Ace
  • 603
  • 2
  • 15
  • 33

4 Answers4

2

To use a custom title in your Toolbar all you need to do is remember is that Toolbar is just a fancy ViewGroup so you can add a custom title like so:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >


     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toolbar Title"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />


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

This means that you can style the TextView however you would like because it's just a regular TextView. So in your activity you can access the title like so:

Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);

setSupportActionBar(toolbar);
mTitle.setText(toolbar.getTitle());

getSupportActionBar().setDisplayShowTitleEnabled(false);
Niraj
  • 903
  • 8
  • 23
  • I would then have to add this into the fragment? Or change the Fragment xml to the Toolbar opening and closing tags? – Ace Dec 26 '19 at 00:17
  • create layout file for Toolbar, then include it in XML wherever needed and check updated java code to use it in any screen. – Niraj Dec 26 '19 at 02:41
  • @Niraj, setDisplayShowTitleEnabled(false) doesn't work for fragments – shehzy Apr 04 '21 at 16:54
0

Better add a layout file for tool bar and center the title there and add dynamically on create or include in your fragment layout file.

Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24
0

I was able to figure out the solution. I will post it incase anyone runs across this issue.

I added the following to the Main Activity xml:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolBar"
    android:elevation="5dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorBlack">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorTransparent"
        android:layout_alignParentStart="true"
        android:layout_gravity="start"
        android:text="@string/log_out"
        android:textAlignment="center"
        android:textColor="@color/lightblue"

        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textColor="@color/colorApp"
        android:textSize="20sp"/>

        <ImageButton
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/ic_add"
            android:tint="@color/lightblue"
            android:layout_alignParentEnd="true"
            android:layout_gravity="end"
            android:background="@color/colorBlack" />


</androidx.appcompat.widget.Toolbar>

I also took away the padding top from the androidx.constraintlayout.widget.ConstraintLayout opening tag in the Main Activity XML

Added the following style to styles.xml:

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

Finally in the Android manifest I added the following theme to the Main Acivity:

android:theme="@style/AppTheme.NoActionBar"
Ace
  • 603
  • 2
  • 15
  • 33
-1

Try below code may works for you

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.abs_layout);
Android Dev
  • 1,496
  • 1
  • 13
  • 25