Mark Denom
Mark Denom

Reputation: 1057

How do I center a logo in the ActionBar?

I am trying to center a logo in the MainActivity ActionBar but when I add this

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.thelogo);
getSupportActionBar().setDisplayUseLogoEnabled(true);

it looks like this enter image description here

Is there no way to center it without having to change my RelativeLayout to a LinearLayout?

Upvotes: 2

Views: 158

Answers (1)

Steve Moretz
Steve Moretz

Reputation: 3128

Since you can't access the view you can't center it unless there's a function for that which I don't think there is that's why I always make my own toolbars use this:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/navigation_background"
        android:layout_gravity="center"/>
</androidx.appcompat.widget.Toolbar>

in your code use now:

...
setSupportActionBar(toolbar);

Make sure you get rid of the default actionbar by changing style to:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

Upvotes: 2

Related Questions