Bringoff
Bringoff

Reputation: 99

Title text alignment on toolbar

I'm using toolbar from support library in my project. And I can't find how to change text alignment. It is at left on default. But I need to move it to the center. I think there is a method setTitleTextAppearance() for it but I don't understand its using.

Upvotes: 2

Views: 10128

Answers (2)

user9050166
user9050166

Reputation:

It's quite easy - you can do it programmatically:

    public class AboutUsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);
        Toolbar toolbar = (Toolbar) findViewById(R.id.devtoolbar_about);
        toolbar.setTitle("About Us");
        toolbar.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        setSupportActionBar(toolbar);
    }
}

The options for alignment are: TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_LEFT, and others. PS: I know this is an old question!

Upvotes: 0

Abhinav Puri
Abhinav Puri

Reputation: 4284

Add your own TextView which can be aligned wherever you want:

<android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/toolbar"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/green">

   <!-- Below will add your text in the center of the toolbar -->
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Your Text"
    android:textColor="#ff0000"
    android:textStyle="bold"/>

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

Upvotes: 4

Related Questions