Center Align the name in the action bar using styles in android.

Hi, I am developing an application for Android. In my application, I use the action bar. I want the center to align my window title in the action bar. I know that this is possible with xml files. but I want to do this with a style so that it applies to every window how to do it. Thank.
+14
android android-actionbar title center-align
Oct 08 '13 at 9:49
source share
4 answers

I know that this is possible with xml files. but I want to do this using a style so that it is applicable for each of my windows, how to do this help.

You are still using xml for this. Just apply the style app broadly, not just one action. Please see the Android documentation for a full description:

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

Then apply your theme for the entire application or individual actions:

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

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

In your action_bar.xml you will 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 will have something like:

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

If you need to have this in every action, you have 2 options:

  • Brute force: put the above line of code in every onCreate action.
  • Extend the action and put the ActionBar initialization code in onCreate. Then expand your own activity class for each action. Just remember to call super () in every onCreate () action.
+8
08 Oct '13 at 9:59 on
source share

A simple method to center an ActionBarTitle without using a custom layout for an actionBar.

But before that, first hide the 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 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); } } 
+29
Jun 20 '14 at 10:08
source share

Unfortunately, SBerg413 does not work for me.

my custom layout

 <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 everything works fine:
http://i.stack.imgur.com/MvQgj.png

UPD: this is an action that I use as a parent for all my actions that should have an 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); } } 
+4
Oct 06 '14 at 8:01
source share

@ Napoleon's answer helped me, but in my case I use the Xamarin ensemble (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); 
+3
Dec 04 '14 at 8:23
source share



All Articles