0

How can I achieve title bar on the centre ? I want its exactly on the blue line that I drew on the picture below. Thanks in advance

Here is my code:

On touch :

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

XML :

     <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:text="Home"
    android:textColor="#ffffff"
    android:id="@+id/mytext"
    android:textSize="18sp" />

   </LinearLayout>

My output : I want its exactly on the blue line that I drew on the picture below.

I want its exactly on the blue line that I drew on the picture below.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
CS9423
  • 21
  • 1
  • 5

2 Answers2

0

Use RelativeLayout instead of LinearLayout. android:layout_gravity doesn't work with LinearLayout.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:text="Home"
        android:textColor="#ffffff"
        android:id="@+id/mytext"
        android:textSize="18sp" />

</RelativeLayout>
Mikhail
  • 800
  • 8
  • 21
0

Just create custom toolbar. add gravity to the textview

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/mypay_purple">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                style="@style/FontNormal"
                android:textColor="@color/mypay_white"
                android:text="Title"
                />

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

in activity

setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    title.setText("Payment");
}
ZeroOne
  • 8,996
  • 4
  • 27
  • 45