I have a custom home icon for my app and I want it to align all the way to the left of the actionbar, so that it's touching the edge of the screen. Is this possible, and if so, how can it be done? I don't see anything that sets the padding or margin to make it align all the way to the left.
Asked
Active
Viewed 5,592 times
7
-
is there a padding or margin attached to the home icon?? if so remove them. Btw show us some code from xml file then many here can help you out. – Jay Mayu Feb 13 '12 at 18:47
-
There is no xml for it. Just added android:icon="@drawable/my_icon" to the manifest and ab.setDisplayShowHomeEnabled(true); ab.setDisplayUseLogoEnabled(false); to the activity – CACuzcatlan Feb 13 '12 at 19:33
1 Answers
5
I finally managed to get this. You need to use a custom actionbar view. It's actually quite easy:
(This is using ActionbarSherlock, it should work with the stock compat library also...)
First in your res/values/themes.xml, set the actionbar display options to "custom":
<item name="android:displayOptions">showCustom</item>
Then create a file called res/layout/actionbar_layout.xml and place in it something like:
<ImageView android:id="@+id/home_icon" android:layout_alignParentLeft="true" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:scaleType="centerCrop" android:layout_width="wrap_content" android:layout_height="fill_parent" /> <!-- Add textviews or whatever you like here to replicate the actionbar title etc. -->
Next, in your activity code add the following:
View mActionCustomView = getSherlockActivity().getLayoutInflater() .inflate(R.layout.actionbar_layout, null); getSherlockActivity().getSupportActionBar().setCustomView( mActionCustomView); ImageView homeIcon = (ImageView)mActionCustomView .findViewById(R.id.home_icon); int abHeight = getSherlockActivity().getSupportActionBar() .getHeight(); homeIcon.setLayoutParams(new RelativeLayout.LayoutParams( abHeight, abHeight));
That's basically it! Let me know if there's anything I left out. There are some nice benefits of having a customisable actionbar view, just stick to the basics and it will look great.

brk3
- 1,524
- 2
- 19
- 31
-
You don't need to set the homeicon layout params, simply set the margins on your @+id/home_icon to 0 (instead of 5dp as shown in example). – scottyab Jun 07 '12 at 12:49
-
@OnuraySahin You can set the same option in code http://developer.android.com/reference/android/app/ActionBar.html#setDisplayShowCustomEnabled%28boolean%29 – brk3 Jun 18 '13 at 14:58