How to set space on left&right of the logo in the actionBar?

2

Background

I want to show a bitmap on the left side of the title of the activity, in the actionbar (toolbar, to be exact), so I used this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setLogo(R.mipmap.ic_launcher);
    ...

The problem

Thing is, the logo seems to have too much margin on its left, while the title text is very near the logo itself, so it looks like this:

enter image description here

This is not symmetrical at all...

This is even worse in case I have a nav drawer:

enter image description here

What I've tried

  1. I tried other sizes of the icon, plus I tried to use a bitmap instead of a drawable/mipmap resource (and I actually need to use a bitmap anyway), as such:

     TypedValue tv = new TypedValue();
     if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
         int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
         Bitmap bitmap = Bitmap.createBitmap(actionBarHeight, actionBarHeight, Config.ARGB_8888);
         Canvas canvas = new Canvas(bitmap);
         final Paint paint = new Paint();
         paint.setColor(0xffff0000);
         canvas.drawPaint(paint);
         getSupportActionBar().setLogo(new BitmapDrawable(getResources(),bitmap));
     }
    

But I still got the same result:

enter image description here

  1. I tried to use this:

     toolbar.setContentInsetsAbsolute(0,0);
    
  2. I tried to use this in the xml tag of the toolbar:

     android:contentInsetLeft="0px"
     android:contentInsetStart="0px"
    

I also tried to play with the other values to its right&end (at least to make the spaces equal in size), but they don't seem to affect the logo margins at all.

  1. Only thing that seem to help is this:

     app:contentInsetStartWithNavigation="0px"
    

But it helps only if there is a navigation-drawer:

enter image description here

In the case of no navigation drawer, I still see extra space, which makes it look like un-even spaces on the left of the logo compared to on its right. I tried to set this value to be negative, but it doesn't do anything in the case of no nav-drawer.

  1. I've also tried to investigate why it has the extra space, using the "layout inspector" tool so that I might be able to force it to have less space, but I couldn't find any padding/margins that will cause this:

enter image description here

The question

How do I avoid this behavior? How can I minimize/set the space on the left&right of the logo?

Why does it occur?

Should I just use a custom view instead?

android
android-actionbar
android-toolbar
graphical-logo
asked on Stack Overflow Oct 9, 2016 by android developer • edited Nov 24, 2020 by Anatoly

1 Answer

4

How can I minimize/set the space on the left&right of the logo ?

Use these attributes in your toolbar to handle margins of the toolbar view.

        android:layout_marginLeft="-16dp"
        app:titleMarginStart="@dimen/your_required_space"

-16dpleaves about 4dp space.

The disadvantage of using layout_marginLeft is that it does not work well when there is another view before the logo or your custom bitmap. For example, hamburger icon in case of Navigation Drawer, up-caret icon (back-arrow) when child activity is launched or navigation drawer is opened. This happens because the logo gets replaced by such views.

In such case, you can use :

        app:contentInsetStartWithNavigation="0dp"

Why does it occur?

Maybe, toolbar class uses already set dimensions which the developers think are the standard dimensions and there is no explicit way to alter them.

Should I just use a custom view instead?

You can use custom views as you've complete control but with custom views, you get custom headaches.

answered on Stack Overflow Oct 10, 2016 by Gurupad Mamadapur • edited Oct 10, 2016 by Gurupad Mamadapur

User contributions licensed under CC BY-SA 3.0