2

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?

Vebbie
  • 1,669
  • 2
  • 12
  • 18
Mark Denom
  • 987
  • 1
  • 8
  • 24

1 Answers1

2

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">
Steve Moretz
  • 2,758
  • 1
  • 17
  • 31
  • Do you create a seperate xml file for the Toolbar? – Mark Denom Feb 09 '19 at 19:14
  • No just put it in your activity xml just like any other layout you use in your activity. – Steve Moretz Feb 09 '19 at 19:15
  • `setSupportActionBar(findViewById(R.id.toolbar));` actually throws an error https://i.imgur.com/JdAVykM.png – Mark Denom Feb 09 '19 at 19:20
  • Yes there are two Toolbars android.widget and android.appcompat.widget just delete The import statement and make sure on the import you choose the latter :) – Steve Moretz Feb 09 '19 at 19:23
  • That's what's funny to me I only have `import android.support.v7.widget.Toolbar;` imported and no other toolbar, but oh well i casted it as a Toolbar now and it should work. – Mark Denom Feb 09 '19 at 19:24
  • Just remove the import android.support.v7.widget.Toolbar; and then the variable becomes red then go click on it press alt+enter and choose import it'll give you two options just choose the other one. – Steve Moretz Feb 09 '19 at 19:25
  • That's the import it needs though but it's not utilizing it until I cast the object – Mark Denom Feb 09 '19 at 19:27
  • Oh yes cause I'm using androidX I messed it up.Yeah it should be cast because findViewById returns a View Object.Ok so it's working now?Is it centered? – Steve Moretz Feb 09 '19 at 19:29
  • It's not, the StackTrace is throwing a lot of errors right now regarding how it already has a ACtionBar etc – Mark Denom Feb 09 '19 at 19:34
  • Your answer combined with this works. https://stackoverflow.com/a/34461978/7808468 – Mark Denom Feb 09 '19 at 19:37
  • Just edit your post and include the errors.I don't think so if you should be getting any errors related to this.I'm right now using 3 toolbars on the same activity with appcompat toolbar. – Steve Moretz Feb 09 '19 at 19:37
  • Please add my comment to your answer so people can see. – Mark Denom Feb 09 '19 at 19:38
  • I always get rid of toolbar the first time.with setting the theme that's why I never faced any error.Updated answer you can accept and upvote it. – Steve Moretz Feb 09 '19 at 19:41
  • @MarkDenom You can accept the answer as well if it you consider it as that :) – Steve Moretz Feb 09 '19 at 19:49