Tamas
Tamas

Reputation: 1765

Actionbar centered title with buttons

I would like to center the title in the action bar, while having action icons next to it(and ignoring them for the gravity setting).

I would like to have this: enter image description here

Instead I have this: enter image description here

Here is my code:

    ActionBar actionBar = getSupportActionBar();

    ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);

    actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.top_bar));
    actionBar.setCustomView(LayoutInflater.from(this).inflate(R.layout.actionbar_layout, null), params); 
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);

My layout:

<?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="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:padding="8dp"
        android:src="@drawable/launcher_icon" />

    <TextView
        android:id="@+id/actionbar_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:maxHeight="1dp"
        android:maxLines="1"
        android:paddingLeft="48dp"
        android:paddingRight="48dp"
        android:text="Bacon ipsum"
        android:textColor="#777777"
        android:textSize="20sp" />

</RelativeLayout>

So my problem is, centering the title only works, if I have 0 actionbar items next to it, as soon as I add an item, the centering gets screwed up. It would also be great, if I could use the up affordance button with it(which also screws up the centering now).

Any help would be appreciated.

I don't wan to implement my own actionbar item/button logic.

Upvotes: 4

Views: 3608

Answers (3)

Risadinha
Risadinha

Reputation: 16666

  1. Remove the launcher icon from your RelativeLayout.
  2. Change the width of the RelativeLayout to wrap_content
  3. Remove:

    actionBar.setDisplayShowCustomEnabled(true); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowTitleEnabled(false);

  4. Add:

    ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
    
    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER);
    
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)
    actionBar.setCustomView(yourInflatedCustomView, lp);
    

Upvotes: 2

Bae
Bae

Reputation: 912

You can try this in the TextView:

<TextView
    android:id="@+id/actionbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:maxHeight="1dp"
    android:maxLines="1"
    android:paddingLeft="48dp"
    android:paddingRight="48dp"
    android:text="Bacon ipsum"
    android:textColor="#777777"
    android:textSize="20sp" />

Upvotes: 3

Celta
Celta

Reputation: 3620

Have you tried to implement to the text this code?

android:layout_centerHorizontal="true"

Upvotes: 0

Related Questions