0

I would like the icon of my app to appear in the center of the actionbar - no other changes to the actionbar are required at all.

I have looked at more comprehensible solutions, such as this one: ActionBar logo centered and Action items on sides

While this is possible, it seems like overkill for such a simple use case. Is there any way it can be achieved through simple styling? I am using the default Holo Light theme, with base API 11.

Community
  • 1
  • 1
csvan
  • 8,782
  • 12
  • 48
  • 91
  • Oops, just clicked on your link. It does seem overkill, had a similar problem, but ended up doing it the overkill way anyways – Lunchbox Apr 10 '14 at 09:57

2 Answers2

2

Why not set a background to your ActionBar with the image in it (like a strip for the Action bar background with your image to its center, Nine patch would be preferred).

Then you can assign this as Action Bar background like,

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

    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>        
</style>

and,

<!-- general styles for the action bar -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">

    <!-- Support library compatibility -->        
    <item name="android:background">@drawable/actionbar</item>// your nine patch image
    <item name="android:icon">@android:color/transparent</item>       
</style>

You can then hide the Title as well. This is not a hack but considering you don't wanna temper ActionBar this will work. Have fun :)

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
0

You can do something like this:

ActionBar action = getActionBar();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.action_menu, null);
action.setCustomView(view);

action_menu.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10sp" >

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:text="Left button" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="The logo" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="Right button" />

</RelativeLayout>
Lunchbox
  • 1,538
  • 2
  • 16
  • 42
  • Thanks for the answer. I know I can do it like this (see linked example), but it is precisely the kind of solution I want to avoid - I just want to do a very small change to the appearance of the actionbar WITHOUT having to implement a custom layout to do so. An acceptable solution would be a modification to the standard theme. – csvan Apr 10 '14 at 09:58
  • 1
    I commented on your question as well. I had to do something similar, but ended up doing it this way. Such a hassle I know – Lunchbox Apr 10 '14 at 09:59