Search code examples
javaandroidandroid-actionbarstylesandroid-toolbar

Android AppCompat ActionBar - Left Align Logo


Within the ActionBar, I am trying to left align the logo and also display the Activity title. I'm finding it difficult to left align the logo.... and also display the logo and the title together.

My Activity is extending AppCompatActivity. I am using the styles.xml to provide the theme to the app. In the styles.xml theme, I have the following:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
   <item name="actionBarStyle">@style/ActionBarLogoTheme</item>
</style>

Which calls the following:

<style name="ActionBarLogoTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
   <item name="logo">@drawable/logo</item>
   <item name="displayOptions">useLogo|showHome|showTitle</item>>

   <item name="android:layout_gravity">left</item>
   <item name="android:gravity">left</item>
</style>

In the above, the 'gravity' and 'layout_gravity' don't seem to take affect, and also the 'showTitle' display option doesn't seem to take affect also. Would anyone be able to help. The following illustrates what it currently looks like, and what I would like it to look like.

Thank you all. enter image description here


Solution

  • If you want to use an ActionBar you can use:

    <style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="displayOptions">showHome|useLogo|showTitle</item>
        <item name="logo">@drawable/....</item>
    </style>
    

    You can also it programmatically:

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setLogo(R.drawable....);
        getSupportActionBar().setTitle("Title");
    

    enter image description here

    But you can also use a Toolbar:

        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_menu_camera);
        setSupportActionBar(toolbar);
    

    enter image description here