-3

enter image description here

I know there are two ways of setting the title of activity. One way is the setting it in the android manifest like this android:label="@string/app_name". Second is programmatically setting in activity class like setTitle("Hello World!"). Both ways are positioned in the left side but how can I put it in the center?

Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
Shamś ŚhEikh
  • 25
  • 1
  • 2
  • 6

3 Answers3

1

Use toolbar

<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/img_header">


    <TextView
        android:id="@+id/activity_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="TITLE"
        android:layout_gravity="center"
        android:textColor="@android:color/white"
        android:textSize="@dimen/dim_20"
        android:typeface="serif" />
</android.support.v7.widget.Toolbar>

set theme NotitleBar in manifest file inside activity tag

android:theme="@android:style/Theme.Translucent.NoTitleBar"

or

setSupportActionBar(findViewById(R.id.toolbar));
Hitesh Gehlot
  • 1,307
  • 1
  • 15
  • 30
1

Try to create custom title by adding text view under toolbar like this

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">
 <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>

call on your activity

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_top);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText("any");
Shashank Verma
  • 284
  • 4
  • 19
1

Try this:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Toolbar"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />

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

In your Activity:

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

mTitle.setText("Your Title");

setSupportActionBar(toolbar);
getSupportActionBar().setTitle(""); // Hide default app name
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61