How to Center Align the Actionbar Title in Android

How to center align the ActionBar title in Android?

To have a centered title in ABS (if you want to have this in the default ActionBar, just remove the "support" in the method names), you could just do this:

In your Activity, in your onCreate() method:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.abs_layout);

abs_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">

<android.support.v7.widget.AppCompatTextView
android:id="@+id/tvTitle"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFFFFF" />

</LinearLayout>

Now you should have an Actionbar with just a title. If you want to set a custom background, set it in the Layout above (but then don't forget to set android:layout_height="match_parent").

or with:

getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.yourimage));

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 center action bar tittle

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");

How to align title at center of ActionBar in default theme(Theme.Holo.Light)

You can create a custom layout and apply it to the actionBar.

To do so, follow those 2 simple steps:

  1. Java Code

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.actionbar);

Where R.layout.actionbar is the following layout.


  1. XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/action_bar_title"
    android:text="YOUR ACTIVITY TITLE"
    android:textColor="#ffffff"
    android:textSize="24sp" />
    </LinearLayout>

It can be as complex as you want. Try it out!

EDIT:

To set the background you can use the property android:background in the container layout (LinearLayout in that case). You may need to set the layout height android:layout_height to match_parent instead of wrap_content.

Moreover, you can also add a LOGO / ICON to it. To do so, simply add an ImageView inside your layout, and set layout orientation property android:orientation to horizontal (or simply use a RelativeLayout and manage it by yourself).

To change the title of above custom action bar dynamically, do this:

TextView title=(TextView)findViewById(getResources().getIdentifier("action_bar_title", "id", getPackageName()));
title.setText("Your Text Here");

How to align action bar title to centre without using custom view

You can align the title to the center when you use ActionBar, but you can use Toolbar to do this.

Toolbar is more useful and easier than ActionBar, you can use this layout to define the center title TextView for you activity:

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="40dip">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/app_name" />

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

And use this code for a back button:

protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_toolbar);

Toolbar toolbar = (Toolbar) findViewById(R.id.single_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if(actionBar != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

You also need to override onCreateOptionsMenu method for the menu, and you can refer to this project : chrisbanes/cheesesquare.

How to make android action bar title center aligned?

I am sure, you are using the below to set your custom theme.

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.abs_layout);

If not, then please add the first line. And the text in your custom layout is centered. (Gravity = Center)

Also I found a similar link here and here. Hope this helps.

Try this out as well.

I was able to accomplish what I wanted by not showing the title of
the activity, and instead using a customView as you suggested. The key
that I missed for a while was you must use the setCustomView(View,
ActionVar.LayoutParams) method in order to get the custom view
centered, simply setting a layout_gravity="center" in the custom view
layout file does not work. Here's a short code snippet showing how I
got it to work:

            TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
null);
ActionBar.LayoutParams params = new
ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);

customView.setText("Some centered text");
getSupportActionBar().setCustomView(customView, params);

This method is working for me on at least 3.2, 2.3, and 2.2 devices.

Source https://groups.google.com/forum/#!msg/actionbarsherlock/A9Ponma6KKI/Llsp41jWLEgJ

Center Align title in action bar using styles in android.

I know it is possible using xml files. but I want to do it using style
so that it will applicable for my every window, how to do this need
help.

You still use xml to do this. Just apply the style application wide and not just to a single activity. Please see the android documentation for a complete description:

https://developer.android.com/training/basics/actionbar/styling.html

Then apply your theme to your entire app or individual activities:

<application android:theme="@style/CustomActionBarTheme" ... />

EDIT: Here's how you'll have to handle the centering of the the title:

In your action_bar.xml you'll have something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="SOME TITLE"
android:textSize="18sp" />
</RelativeLayout>

Then in your onCreate() you'll have something like:

getSupportActionBar().setCustomView(R.layout.action_bar);

If you need to have this in every activity, you have 2 choices:

  1. The brute force way: Put the above line of code in every Activity's onCreate.
  2. Extend Activity and put your ActionBar initialization code in the onCreate. Then Extend your custom Activity class for each Activity. Just don't forget to call the super() in each Activity's onCreate().

How to align Title in center of Actionbar with back button in fragment

So finally I found a trick to tackle this problem and it worked

setTitle("Sign In");
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setHasOptionsMenu(true);

here is my setTitle method

 public void setTitle(String title) {
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView textView = new TextView(getActivity());
textView.setText(title);
textView.setTextSize(20);
textView.setTypeface(null, Typeface.BOLD);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);


textView.setTextColor(getResources().getColor(R.color.black));
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
((AppCompatActivity) getActivity()).getSupportActionBar().setCustomView(textView);
}

and here I created a menu item with transparent image

 @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.blank_menu,menu);
super.onCreateOptionsMenu(menu, inflater);
}

here is my blank_menu.xml

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:title=" "
app:showAsAction="always|withText"
android:icon="@drawable/transparent"
>
</item>
</menu>

and now its giving output like this
Sample Image



Related Topics



Leave a reply



Submit