Android toolbar center title and custom font
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="?android: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);
Android How to center title of top actionBar?
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"
How to align title at layout center in TopAppBar?
With Material2 you have to use the other constructor of TopAppBar
that has no pre-defined slots for content, allowing you to customize the layout of content inside.
You can do something like:
val appBarHorizontalPadding = 4.dp
val titleIconModifier = Modifier.fillMaxHeight()
.width(72.dp - appBarHorizontalPadding)
TopAppBar(
backgroundColor = Color.Transparent,
elevation = 0.dp,
modifier= Modifier.fillMaxWidth()) {
//TopAppBar Content
Box(Modifier.height(32.dp)) {
//Navigation Icon
Row(titleIconModifier, verticalAlignment = Alignment.CenterVertically) {
CompositionLocalProvider(
LocalContentAlpha provides ContentAlpha.high,
) {
IconButton(
onClick = { },
enabled = true,
) {
Icon(
painter = painterResource(id = R.drawable.ic_add_24px),
contentDescription = "Back",
)
}
}
}
//Title
Row(Modifier.fillMaxSize(),
verticalAlignment = Alignment.CenterVertically) {
ProvideTextStyle(value = MaterialTheme.typography.h6) {
CompositionLocalProvider(
LocalContentAlpha provides ContentAlpha.high,
){
Text(
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center,
maxLines = 1,
text = "Hello"
)
}
}
}
//actions
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
Row(
Modifier.fillMaxHeight(),
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically,
content = actions
)
}
}
}
With Material3 you can simply use the CenterAlignedTopAppBar
:
CenterAlignedTopAppBar(
title = { Text("Centered TopAppBar") },
navigationIcon = {
IconButton(onClick = { /* doSomething() */ }) {
Icon(
imageVector = Icons.Filled.Menu,
contentDescription = "Localized description"
)
}
}
)
Android - How to center a title (TextView) in a toolbar?
There are a couple of ways to solve your problem. The quickest one would be to change your TextView as follows.
<TextView
android:id="@+id/toolbar_title"
android:layout_width="match_parent" // Fill parent
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Sign Up"
android:gravity="center" // Make the text center aligned
android:textColor="#ffffff"
android:textSize="21dp"
android:textStyle="bold" />
</LinearLayout>
This makes sure that your text would cover the entire space next to the icon and center the text within it. You dont even need a LinearLayout
for that. It can be removed and it'll still work.
How to set Title in center of Toolbar in fragment
You can put text view
inside toolbar
like this :
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent" >
<TextView
android:id="@+id/title"
android:text="Title"
android:textSize="20dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.appcompat.widget.Toolbar>
and if you want to set the text view
text to your app title use this :
TextView t = (TextView) findViewById(R.id.title) ;
t.setText(getTitle()) ;
The result :
Toolbar title not in center when Back Button is enable
Add a TextView inside the Toolbar & don't forget to set the following attribute inside your TextView.
android:layout_marginRight="?android:attr/actionBarSize"
OR
android:layout_marginEnd="?android:attr/actionBarSize"
code snippet:
<android.support.v7.widget.Toolbar
android:id="@+id/custom_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@android:color/holo_red_dark">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textColor="@android:color/white"
android:textSize="20sp"
android:layout_marginRight="?android:attr/actionBarSize"
android:gravity="center"/>
</android.support.v7.widget.Toolbar>
Refer to this tutorial for more information.
Related Topics
Android Update Textview in Thread and Runnable
Phonegap Eclipse Issue - Eglcodeccommon Glutilsparamsize: Unknow Param Errors
Android Exception: Did You Forget to Call 'Public Void Setup (Localactivitymanager Activitygroup)'
Autocompletetextview with Custom Adapter and Filter
Rotating Phone Quickly 180 Degrees, Camera Preview Turns Upside Down
How to Keep an Alertdialog Open After Button Onclick Is Fired
Differencebetween @Id and @+Id
Upgrade Realm in an Android Project
Kotlin: Whats Does "Return@" Mean
Android Listview with Onclick Items
Android Layout Folders: Layout, Layout-Port, Layout-Land
Transparent Alertdialog Has Black Background
Android Cancel Toast When Exiting the App and When Toast Is Being Shown
Cmusphinx Pocketsphinx - Recognize All (Or Large Amount) of Words