2

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
  • 41
  • 1
  • 1
  • 7
  • 4
    Possible duplicate of [How to center align the ActionBar title in Android?](http://stackoverflow.com/questions/12387345/how-to-center-align-the-actionbar-title-in-android) – Nitesh Sep 15 '16 at 11:11

4 Answers4

5

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.

Kenny Dabiri
  • 353
  • 3
  • 22
L. Swifter
  • 3,179
  • 28
  • 52
1

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("");
Tushar Monirul
  • 4,944
  • 9
  • 39
  • 49
0

It seems there is no way to do this without custom view. You can get the title view:

View decor = getWindow().getDecorView();
TextView title = (TextView) decor.findViewById(getResources().getIdentifier("action_bar_title", "id", "android"));

But changing of gravity or layout_gravity doesn't have an effect. The problem in the ActionBarView, which layout its children by itself so changing of layout params of its children also doesn't have an effect. To see this excecute following code:

ViewGroup actionBar = (ViewGroup) decor.findViewById(getResources().getIdentifier("action_bar", "id", "android"));
View v = actionBar.getChildAt(0);
ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
p.gravity= Gravity.CENTER;
v.setLayoutParams(p);
v.setBackgroundColor(Color.BLACK);
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
0

This solution programmatically adds a centered TextView to the Toolbar:

TextView(this).apply {
    text = getString(R.string.settingsLocaleTitle)
    typeface = Typeface.DEFAULT_BOLD
    textSize = 20f
    layoutParams = Toolbar.LayoutParams(
        LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT
    ).apply {
        gravity = Gravity.CENTER
    }
}.also { toolbar.addView(it) }
6rchid
  • 1,074
  • 14
  • 19