How to align action bar title to centre without using custom view

11,490

Solution 1

You can align the title to the center when you use ActionBar, but you can use Toolbar to do this.

Toolbar is more useful and easier than ActionBar, you can use this layout to define the center title TextView for you activity:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="40dip">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/app_name" />

</android.support.v7.widget.Toolbar>

And use this code for a back button:

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.activity_toolbar);

    Toolbar toolbar = (Toolbar) findViewById(R.id.single_toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    if(actionBar != null)
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

You also need to override onCreateOptionsMenu method for the menu, and you can refer to this project : chrisbanes/cheesesquare.

Solution 2

Ok, You can try this:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" >
        <TextView
            android:textColor="#fff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_gravity="center"
            android:text="@string/app_name" />
    </android.support.v7.widget.Toolbar>

And remember to add this line in your activity's java code:

getSupportActionBar.setTitle("");
Share:
11,490
Rajesh Koritela
Author by

Rajesh Koritela

Updated on June 28, 2022

Comments

  • Rajesh Koritela
    Rajesh Koritela about 2 years

    I want to align the action bar title to centre without the help of custom view . I would appreciate any help.

    Without using the custom view, modifying only default action bar title

  • Rajesh Koritela
    Rajesh Koritela almost 8 years
    gud one but I have to add back button and menu options in this custom action bar? @L.Swifter
  • L. Swifter
    L. Swifter almost 8 years
    @RajeshKoritela you can add the back button and the menu options in Toolbar, the way to set the button and the menu is the same with ActionBar.
  • Rajesh Koritela
    Rajesh Koritela almost 8 years
    Can you please provide the code if possible? @L.Swifter
  • L. Swifter
    L. Swifter almost 8 years
    I add some code for the back button and the menu, and you can check the repo for more information. Hope this can help you :) @RajeshKoritela