Center Align title in action bar using styles in android.
Asked Answered
S

4

16

Hi I am developing android application. In my application I am using action bar. I want to make center align my title of window in action-bar. 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. Thank you.

Synecious answered 8/10, 2013 at 9:49 Comment(2)
I tried with xml its working but I want to do it with styles. because for every window my title will be in the middle. So i want to do it with styles.Synecious
can any one help #31803963Afterbrain
J
9

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().
Jitney answered 8/10, 2013 at 9:59 Comment(5)
thank you for help I checked your given link. Actually I am able to set color size of title but not able to do senter align. YOu have any idea regarding this. Need help. thank you.Synecious
Updated my answer. Hope it helps you.Jitney
thank you for help. I did in same way. But still I feel I am not allow to center it through styles .:(Synecious
same issue #31803963Afterbrain
Does my custom styles in style.xml affect on this custom action bar too?Wilmoth
T
30

Simple method to center ActionBarTitle without using custom layout for actionBar.

But before that first hide up Icon as :

myActionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));

private void centerActionBarTitle() {
    int titleId = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        titleId = getResources().getIdentifier("action_bar_title", "id", "android");
    } else {
        // This is the id is from your app's generated R class when 
        // ActionBarActivity is used for SupportActionBar
        titleId = R.id.action_bar_title;
    }

    // Final check for non-zero invalid id
    if (titleId > 0) {
        TextView titleTextView = (TextView) findViewById(titleId);
        DisplayMetrics metrics = getResources().getDisplayMetrics();

        // Fetch layout parameters of titleTextView 
        // (LinearLayout.LayoutParams : Info from HierarchyViewer)
        LinearLayout.LayoutParams txvPars = (LayoutParams) titleTextView.getLayoutParams();
        txvPars.gravity = Gravity.CENTER_HORIZONTAL;
        txvPars.width = metrics.widthPixels;
        titleTextView.setLayoutParams(txvPars);
        titleTextView.setGravity(Gravity.CENTER);
    }
}
Tribulation answered 20/6, 2014 at 10:8 Comment(5)
Repeating the same with action_bar_subtitle also centers the Action Bar subtitle.Level
Works, but if the ActionBar has MenuItems, the title moves some pixels to the left.Lackluster
If the title is just a little bit off centered after doing this, just play around with titleTextView left padding.Secrete
Where should I call this method? I'm calling in onCreate, as the last instruction, and I'm getting a crashBanner
This doesn't seem to work for me. I keep getting null for the titleTextView. I tried calling this method in onCreate, onResume and even made a UI button to trigger this method, but they all end up the same. What am I doing wrong?Orthopedic
J
9

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().
Jitney answered 8/10, 2013 at 9:59 Comment(5)
thank you for help I checked your given link. Actually I am able to set color size of title but not able to do senter align. YOu have any idea regarding this. Need help. thank you.Synecious
Updated my answer. Hope it helps you.Jitney
thank you for help. I did in same way. But still I feel I am not allow to center it through styles .:(Synecious
same issue #31803963Afterbrain
Does my custom styles in style.xml affect on this custom action bar too?Wilmoth
E
5

Unfortunally, SBerg413's way doesn't work for me.

my custom layout is

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/custom_action_bar_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="test"
        android:textColor="@color/action_bar_title_color"
        android:textSize="@dimen/action_bar_title_size" />
</RelativeLayout>

And all works perfect:
https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKnMAWVMrKmltX1ywKmMva3/MvQgj.png

UPD: this activity i use like a parent for all my activities, that should have the action bar.

public class ActionBarCustomizationActivity extends ActionBarActivity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.getSupportActionBar().setDisplayShowCustomEnabled(true);

        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.custom_action_bar, null);

        TextView titleTextView = (TextView) v.findViewById(R.id.custom_action_bar_title);
        titleTextView.setText(this.getTitle());
        titleTextView.setTypeface(App.getInstance().getActionBarTypeFace());

        this.getSupportActionBar().setCustomView(v);
    }
}
Emulsoid answered 6/10, 2014 at 20:1 Comment(0)
S
3

@Napolean's answer helped me, but in my case I am using Xamarin android (monodroid). Here's the translation of his code in C#:

var titleId = Resources.GetIdentifier("action_bar_title", "id", "android");
var titleTextView = FindViewById<TextView>(titleId);
var layoutParams = (LinearLayout.LayoutParams) titleTextView.LayoutParameters;
layoutParams.Gravity = GravityFlags.CenterHorizontal;
layoutParams.Width = Resources.DisplayMetrics.WidthPixels;
titleTextView.LayoutParameters = layoutParams;
titleTextView.Gravity = GravityFlags.Center;
//Added padding because it is slightly off centered.
titleTextView.SetPadding(100, 0,0,0);
Secrete answered 4/12, 2014 at 8:23 Comment(1)
Where are you calling this code from? I have done it from the main activity, but when OnConfigurationChange fires from a rotation, the centering gets reset.Medley

© 2022 - 2024 — McMap. All rights reserved.