How to Align Title at Center of Actionbar in Default Theme(Theme.Holo.Light)

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 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

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

Center aligning of title in action bar of activity and fragments

Try adding the following to your TextView of your custom ActionBar layout to center the text.

android:layout_gravity="center"

Also, to explain, when you set gravity="center" that just centers the text in the TextView layout.

Next, your icon hides because your custom layout overlaps the icon for the navigation drawer (siding menu). To fix this, the easiest way is to set a margin for your entire layout (in your case, the relative layout of your custom actionbar layout). I think 30dp should be enough to cover all screen sizes.

android:layout_marginRight="30dp"

How to centre text in Sherlock Action Bar with icon and sliding menu

You can do it using titleTextStyle attribute of ActionBar.

In your styles.xml, in the App theme, set actionBarTabSyle to a custom style as,

 <item name="titleTextStyle">@style/TitleTextStyle</item>

And heres the CustomActionBarTabs,

<!-- action bar tab styles -->
<style name="TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/actionbar_text</item>
<item name="android:gravity">center</item> //center the title
</style>

Using this you can set various styles to the Title Text like color etc. Remember this will only center the text for the space provided to the Title within the ActionBar. In case this doen't help much you can always create your own custom view and add it to ActionBar as,

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

In case you want to create your own layout, follow these 2 simple steps:

  1. Java Code

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

    Where R.layout.actionbar is the following XML.

  2. 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:text="YOUR ACTIVITY TITLE"
    android:textColor="#ffffff"
    android:textSize="24sp" />
    </LinearLayout>

Source.



Related Topics



Leave a reply



Submit