Center title of activity in android action bar

10,528

I also used actionbar sherlock and i set header position by this method. you can set a custom textview and can set its gravity to center. use the below method and tell me.

    private void setHeader(){
    LayoutInflater linflater = (LayoutInflater)MainAccountActivity.context.getSystemService(MainAccountActivity.context.LAYOUT_INFLATER_SERVICE);
    View GalleryTitleView = linflater.inflate(R.layout.layout_header, null);

    galleryTitleTv = (TextView) GalleryTitleView
            .findViewById(R.id.GalleryTitleTv);
    galleryTitleTv.setText(ITWAppUIConstants.TAB_AGENDA);

    ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.FILL_PARENT,
            ActionBar.LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
    GalleryTitleView.setLayoutParams(lp);
    getSupportActionBar().setCustomView(GalleryTitleView);
  }

and here is the layout i used:

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

<TextView
    android:id="@+id/GalleryTitleTv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingLeft="10dip"
    android:textColor="@color/white"
    android:textSize="20dip"
    android:textStyle="bold" />

Share:
10,528
Cote Mounyo
Author by

Cote Mounyo

Updated on June 04, 2022

Comments

  • Cote Mounyo
    Cote Mounyo almost 2 years

    Right now the title of my activity shows to the left as < Title and then the other menu item shows to the right. I want to center my title and leave out the <. How do I do that? I am using typical menu which I call using

    public boolean onOptionsItemSelected(MenuItem item) 
    

    and

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.yesterday, menu);
        return true;
    }
    

    EDIT:

    I managed to get it to show using

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_yesterday);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,   R.layout.yesterday_title_bar);
    

    where yesterday_title_bar is a relativeLayout

    But it's showing half the height of the views. So I created the following style. But when I apply the style in the manifest. The manifest says it cannot find the resource.

    <style name="CustomWindowTitleBackground">
            <item name="android:background">#323331</item>
        </style>
    
        <style name="CustomTheme" parent="android:Theme">
            <item name="android:windowTitleSize">65dip</item>
            <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
        </style>
    

    Manifest:

    <activity
            android:name="com.company.YesterdayActivity"
            android:theme="@android:style/CustomTheme" >
        </activity>
    
  • Cote Mounyo
    Cote Mounyo almost 11 years
    Do you know how to get the theme from affecting everything in that xml/activity? All my child views are different now. My EditText for example is taller.
  • Tonithy
    Tonithy almost 11 years
    Thats odd. After you've instantiated the actionBar?
  • Cote Mounyo
    Cote Mounyo almost 11 years
    Yes. Note: I had to use getActionBar() instead of getSupportActionBar()
  • Cote Mounyo
    Cote Mounyo almost 11 years
    The custom theme distorts the rest of the layout. If I move it from the manifest to the specific custom layout, it does not even take effect.
  • Cote Mounyo
    Cote Mounyo almost 11 years
    It crashed: cannot combine custom with other features
  • Tonithy
    Tonithy almost 11 years
    @CoteMounyo Honestly, I can't remember why I left that line in there. Just remove it. The title will exist in your custom layout anyway. Just set it in that TextView. but using the non-support ActionBar may have been at issue. Not sure why...
  • Tonithy
    Tonithy almost 11 years
    Also, you need to setDisplayOptions so that it knows to use the custom view... I just edited the original post.