Is it possible to center the ActionBar text?

1

I wanted to know if it is possible to center the ActionBar text, by default it comes to the left.

By default:

Centered (This is how I would like to put it):

Is it possible? Thanks!

Edit 2:

MainActivity.java

package prueba.otrointento;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

    ImageButton btn1;
    ImageButton btn2;
    ImageButton btn3;
    ImageButton btn4;
    ImageButton btn5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = (ImageButton) findViewById(R.id.btn1);
        btn2 = (ImageButton) findViewById(R.id.btn2);
        btn3 = (ImageButton) findViewById(R.id.btn3);
        btn4 = (ImageButton) findViewById(R.id.btn4);
        btn5 = (ImageButton) findViewById(R.id.btn5);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent boton1 = new Intent(MainActivity.this, MainActivity.class);
                startActivity(boton1);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent boton2 = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(boton2);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent boton3 = new Intent(MainActivity.this, Main3Activity.class);
                startActivity(boton3);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        });

        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent boton4 = new Intent(MainActivity.this, Main4Activity.class);
                startActivity(boton4);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        });

        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent boton5 = new Intent(MainActivity.this, Main5Activity.class);
                startActivity(boton5);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        });
    }
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="prueba.otrointento">

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
        android:name=".Main2Activity"
        android:label="Prueba1"
        android:parentActivityName=".MainActivity" >
        <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity
            android:name=".Main3Activity"
            android:label="Prueba2"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity
            android:name=".Main4Activity"
            android:label="Prueba3"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity
            android:name=".Main5Activity"
            android:label="Diseño Gráfico"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
    </application>

</manifest>

layout:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_above="@+id/toolbar">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="236dp"
            app:srcCompat="@drawable/des_1"
            android:id="@+id/des_1"
            android:layout_above="@+id/toolbar"
            android:layout_alignParentStart="true"
            android:scaleType="fitXY" />

        <ImageView
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/des_2"
            android:id="@+id/des_2"
            android:scaleType="matrix"
            android:textAlignment="viewStart"
            android:layout_width="match_parent" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="236dp"
            app:srcCompat="@drawable/des_3"
            android:id="@+id/des_3"
            android:cropToPadding="false"
            android:scaleType="fitXY" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="236dp"
            app:srcCompat="@drawable/des_4"
            android:id="@+id/des_4"
            android:scaleType="fitXY" />

    </LinearLayout>
</ScrollView>

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="?attr/actionBarTheme"
    android:minHeight="73dp"
    android:id="@+id/toolbar"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:elevation="0dp" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn1"
    android:layout_marginEnd="20dp"
    android:background="#00000000"
    android:src="@drawable/btn1_puls_1"
    app:srcCompat="@drawable/menu_des1_puls"
    android:layout_alignTop="@+id/btn2"
    android:layout_toStartOf="@+id/btn2" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn2"
    android:background="#00000000"
    android:layout_marginBottom="14dp"
    android:layout_marginEnd="24dp"
    android:layout_alignParentBottom="true"
    android:layout_toStartOf="@+id/btn3"
    android:src="@drawable/btn2_puls_2"
    app:srcCompat="@drawable/menu_rep1" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn3"
    android:background="#00000000"
    android:layout_alignTop="@+id/btn2"
    android:layout_centerHorizontal="true"
    android:src="@drawable/btn3_puls_2"
    app:srcCompat="@drawable/menu_sm1" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn4"
    android:background="#00000000"
    android:layout_marginStart="23dp"
    android:layout_alignTop="@+id/btn3"
    android:layout_toEndOf="@+id/btn3"
    android:src="@drawable/btn4_puls_2"
    app:srcCompat="@drawable/menu_ip1" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn5"
    android:background="#00000000"
    android:layout_marginStart="22dp"
    android:src="@drawable/btn5_puls_2"
    app:srcCompat="@drawable/menu_dg1"
    android:layout_alignTop="@+id/btn4"
    android:layout_toEndOf="@+id/btn4" />

Log error:

12-18 17:49:18.665 1604-1604/prueba.otrointento E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: prueba.otrointento, PID: 1604
                                                                    Theme: themes:{default=overlay:com.saalim.cm.theme.dusk, iconPack:com.saalim.cm.theme.dusk, fontPkg:com.saalim.cm.theme.dusk, com.android.systemui=overlay:com.saalim.cm.theme.dusk, com.android.systemui.navbar=overlay:com.saalim.cm.theme.dusk}
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{prueba.otrointento/prueba.otrointento.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
                                                                        at prueba.otrointento.MainActivity.onCreate(MainActivity.java:24)
                                                                        at android.app.Activity.performCreate(Activity.java:6251)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5461) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102) 
    
asked by UserNameYo 18.12.2016 в 01:18
source

2 answers

3

Try:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().setTitle(Html.fromHtml("<p align=center>Diseño</p>"));

EDITING

Try this, it is a custom layout, in the background what you do is define in a particular way how your ActionBar will be and in this case, a TextView that will be your title and centered as you want it.

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

Here I leave the layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Diseño grafico"
    android:textColor="#000000"
    android:id="@+id/mytext"
    android:textSize="18sp" />

</LinearLayout>
    
answered by 18.12.2016 / 17:03
source
4

How to center a title on a Toolbar.

The way to do this is through the property android:layout_gravity of the view that will contain the title, in this case the TextView .

This property must be defined as "center", so that the text is perfectly centered.

 android:layout_gravity="center"

Example:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

     <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Titulo del Toolbar"
        android:layout_gravity="center" />

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

This way your title would be perfectly centered.

    
answered by 23.12.2016 в 00:32