Center App ActionBar Title

It simply centers the ActionBar’s Title textview without using any custom layout.

This post is also the solution for accessing and customizing actionBar title For All android Api levels.

But before that you will also need to set invisible your Up Icon as:

myActionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));
// --------------------------------------------------------------- //
/**
* This method simply centers the textview without using custom layout * for ActionBar.
*/
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 titleTxvPars = (LayoutParams) titleTextView.getLayoutParams();
titleTxvPars.gravity = Gravity.CENTER_HORIZONTAL;
titleTxvPars.width = metrics.widthPixels;

titleTextView.setLayoutParams(titleTxvPars);
titleTextView.setGravity(Gravity.CENTER);
}
}