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?
Asked
Active
Viewed 2,798 times
-3
-
if you used toolbar, then use texttitle appereance – Divyesh Patel Apr 18 '17 at 09:45
-
2search on google .what you tried so far ?? – IntelliJ Amiya Apr 18 '17 at 09:45
-
Possible duplicate of [How to make android action bar title center aligned?](http://stackoverflow.com/questions/21770428/how-to-make-android-action-bar-title-center-aligned) – PEHLAJ Apr 18 '17 at 09:47
-
you can do this if you are using toolbar:http://stackoverflow.com/a/26548766/7457753 – AKSHAY MANAGOOLI Apr 18 '17 at 09:47
3 Answers
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