remove padding around action bar left icon on Android 4.0+
Digging into AOSP sources, it seems the code involved is in com.android.internal.widget.ActionBarView.java
. In particular the relevant part is the onLayout()
method of the inner class ActionBarView$HomeView
, partially reported below (lines 1433-1478):
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
...
final LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams();
final int iconHeight = mIconView.getMeasuredHeight();
final int iconWidth = mIconView.getMeasuredWidth();
final int hCenter = (r - l) / 2;
final int iconTop = Math.max(iconLp.topMargin, vCenter - iconHeight / 2);
final int iconBottom = iconTop + iconHeight;
final int iconLeft;
final int iconRight;
int marginStart = iconLp.getMarginStart();
final int delta = Math.max(marginStart, hCenter - iconWidth / 2);
if (isLayoutRtl) {
iconRight = width - upOffset - delta;
iconLeft = iconRight - iconWidth;
} else {
iconLeft = upOffset + delta;
iconRight = iconLeft + iconWidth;
}
mIconView.layout(iconLeft, iconTop, iconRight, iconBottom);
}
the same widget use this layout, defined in res/layout/action_bar_home.xml
:
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="com.android.internal.widget.ActionBarView$HomeView"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView android:id="@android:id/up"
android:src="?android:attr/homeAsUpIndicator"
android:layout_gravity="center_vertical|start"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="-8dip" />
<ImageView android:id="@android:id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dip"
android:layout_marginTop="@android:dimen/action_bar_icon_vertical_padding"
android:layout_marginBottom="@android:dimen/action_bar_icon_vertical_padding"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
</view>
According to sources the icon is shown in the Imageview
with id=android.R.id.home
. The onLayout()
method reported above takes account of the ImageView
margins defined in the layout, which can't be set via theme/style override because they use the value @android:dimen/action_bar_icon_vertical_padding
.
All you can do is to get rid of those values at runtime, and set them according your needs:
simply retrieve the ImageView
and set its top and bottom margins to 0
. Something like this:
ImageView icon = (ImageView) findViewById(android.R.id.home);
FrameLayout.LayoutParams iconLp = (FrameLayout.LayoutParams) icon.getLayoutParams();
iconLp.topMargin = iconLp.bottomMargin = 0;
icon.setLayoutParams( iconLp );
EDIT: I've just realized I didn't cover how to get rid of left padding. Solution below.
Left padding on actionbar is affected by the Navigating Up behavior of actionbar icon. When that is disabled (via ActionBar.setDisplayHomeAsUpEnabled(false)
) the left/up indicator is gone, but a left padding is used as well. A simple solution:
- enable the actionbar up navigation using
ActionBar.setDisplayHomeAsUpEnabled(true)
in order to consider the indicator view in the layout process - force the drawable used as up indicator in your
res/values-v14/styles.xml
tonull
eg:
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
<item name="android:homeAsUpIndicator">@null</item>
</style>
How to remove padding on left of logo in action bar?
You can use content inset feature.
The left inset is caused by Toolbar's contentInsetStart which by default is 16dp.
Try this
<android.support.v7.widget.Toolbar
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" />
And add this at your root.
xmlns:app="schemas.android.com/apk/res-auto";
Try this in your android code
Toolbar.setContentInsetStartWithNavigation(0);
In worst case try this.
add below code in your activity onCreate.
ImageView view = (ImageView)findViewById(android.R.id.home);
view.setPadding(10, 0, 0, 0);
Let me know if it works.
Android: remove left margin from actionbar's custom layout
If you are adding the Toolbar
via XML, you can simply add XML attributes to remove content insets.
<android.support.v7.widget.Toolbar
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" />
Padding between ActionBar's home icon and title
I adapted Cliffus answer and assigned the logo-drawable in my actionbar style definition, for instance like this in res/style.xml:
<item name="android:actionBarStyle">@style/MyActionBar</item>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#3f51b5</item>
<item name="android:titleTextStyle">@style/ActionBar.TitleText</item>
<item name="android:textColor">#fff</item>
<item name="android:textSize">18sp</item>
<item name="android:logo">@drawable/actionbar_space_between_icon_and_title</item>
</style>
The drawable looks like Cliffus' one (here with the default app launcher icon) in res/drawable/actionbar_space_between_icon_and_title.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_launcher"
android:right="20dp"/>
</layer-list>
In the android_manifest.xml you can still set a different app icon (launcher icon on 'desktop'. Any different logo definition here are visible in activities without an action bar.
Related Topics
Android Retrofit - Onprogressupdate for Showing Progress Notification
Difference of Setvalue() & Postvalue() in Mutablelivedata
Endless Recyclerview with Progressbar for Pagination
Programmatically Collapse a Group in Expandablelistview
Missing Styles. Is the Correct Theme Chosen for This Layout
Android Java - Joda Date Is Slow
How to Avoid Delay in Android Gcm Messages/Change Heartbeat
How to Fix "Failed to Sync Vcpu Reg" Error
Creating an Empty Bitmap and Drawing Though Canvas in Android
Placing a Textview on Top of Imageview in Android
Where Does Android View.Scrollto(X, Y) Scroll To
Remove Padding Around Action Bar Left Icon on Android 4.0+
Android Fab Icon Always Black with Materialcomponents Theme
Onclick Not Triggered on Linearlayout with Child
Get Android Google Analytics Referrer Tag
What's the "Dot" for When Registering an Activity
How to Resolve Dependency for ':App@Debug/Compileclasspath': Could Not Resolve