0

I have a mobile project using android studio. I wonder how to make my logo centeren in action bar. after I put an item in action bar, the logo is not centered.

screenshot

and here is my custom action bar view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_actionbar_relative_layout_main"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="vertical"
    android:background="@color/transparent"
    android:enabled="false">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        android:padding="10dp"
        android:src="@drawable/ic_logo"
        android:contentDescription="@string/logo" />
</LinearLayout>

I put my item programaticaly in activity class

supportActionBar?.apply {
            displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
            setDisplayShowCustomEnabled(true)
            setCustomView(R.layout.custom_action_bar)
            elevation = 3f
        }
Hadisyah
  • 34
  • 4
  • lol nice - well it is centred, it's just that when you add a menu to it, that icon takes up a bit of space (and so will any others if you allow them into the bar) so the image is centred in the remaining space. I don't know offhand, but I assume the menu icon is 48dp, so try adding that amount of margin to the other side (you've got 5dp at the moment). You can use the Layout Inspector to check it out on a running emulator or whatever – cactustictacs Apr 24 '21 at 01:01
  • i've tried adding space using margin but when I use it in different device the margin is diferent too – Hadisyah Apr 24 '21 at 07:39
  • There's a few different approaches here: https://stackoverflow.com/questions/29443604/android-how-to-center-title-in-toolbar mostly about trying to set gravity values to ``center``, and someone has an example of setting it in code (by working out the width of the screen and setting the position from that) – cactustictacs Apr 24 '21 at 15:50

0 Answers0