I think this is a very pertinent question in Android development, because it is not enough to try to apply a Gravity
to it, because Toolbar
, being ViewGroup
, is occupied by other elements (actions buttons, por example) that end up robbing the title space of it. If you apply a Gravity
directly to center the title of the Toolbar , it has gotten a bit crooked because of other views / strong>.
I use a rather interesting method and it works in all cases. First, I create a component to serve as a title for my Toolbar
.
res / layout / toolbar_custom_content.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbarTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/app_name"
android:textColor="#212121"
android:textSize="16sp"
/>
You can modify this component any way you want. Color, text ... is all by itself.
Now, normally add your Toolbar to your layout, without making any changes. Just add the View .
res / layout / activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context="togglebutton.cursoandroid.com.togglebutton.MainActivity"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?android:actionBarSize"
android:title="@string/app_name"
app:elevation="1dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
Ready. Now you already have your main layout built, let's go to the code.
MainActivity class
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setupToolbar();
}
private void setupToolbar() {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false); // remove o título original da Toolbar
getSupportActionBar().setDisplayShowCustomEnabled(true); // permite que possamos adicionar custom layouts na toolbar
View viewTitle = getLayoutInflater().inflate(R.layout.toolbar_custom_view, null);
// Lembra da TextView que criamos anteriormente para ser usado como título?
// Então, iremos carregar ela, configurar algumas coisas e depois mandar para a Toolbar.
ActionBar.LayoutParams viewParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
// Nesta parte estamos carregando alguns parâmetro de configuração
// Eles serão aplicados em nossa TextView, para que ela possa estar corretamente centralizada.
viewParams.gravity = viewParams.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK or Gravity.CENTER_HORIZONTAL
// Pegamos a 'gravity' atual da view e aplicamos o operador bitwise AND com o inverso dos bits de Gravity.HORIZONTAL_GRAVITY_MASK
// Isto resolve o problema do título estar com um espaço para a direita quando há um botão de voltar na Toolbar, por exemplo
getSupportActionBar().setCustomView(viewTitle, viewParams);
getSupportActionBar().setDisplayOptions(
getSupportActionBar().getDisplayOptions(),
ActionBar.DISPLAY_SHOW_CUSTOM
);
}
}
Okay, everything's settled. Your Toolbar has a properly centralized title and you will no longer have problems with incorrect centering. You can test the common way, for example, try adding a back button in Toolbar
and then centering a TextView
. You will notice that this has been more aligned to the right, rather than being in the center. This is because of the space occupied by some views.
I understand the answer got a little big and that approach too. But know that this method is efficient and you can modify it to avoid both boilerplate . You do not have to do this in all your activities , just do an abstract class that does what we did here in this answer. The rest is history.