Zbarcea Christian Zbarcea Christian - 4 months ago 51
Android Question

Android: remove left margin from actionbar's custom layout

I am using a custom actionbar view, and as you can see in the screenshot below, there is a blank gray space in the actionbar. I want to remove it.

enter image description here

What have I done:


res/values-v11/styles.xml


<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/ActionBarStyle</item>
<item name="actionBarStyle">@style/ActionBarStyle</item>
</style>



res/values/my_custom_actionbar.xml


<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="android:height">60dp</item>
</style>
</resources>



Manifest


<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/AppName"
android:theme="@style/AppBaseTheme" >
<!-- activities... etc -->
</application>



MainActivity


public void onCreate(Bundle bundle) {
super.onCreate(bundle);

ActionBar actionbar = getSupportActionBar();

actionbar.setDefaultDisplayHomeAsUpEnabled(false);
actionbar.setDisplayHomeAsUpEnabled(false);
actionbar.setDisplayShowCustomEnabled(true);
actionbar.setDisplayShowHomeEnabled(false);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayUseLogoEnabled(false);
actionbar.setHomeButtonEnabled(false);

// Add the custom layout
View view = LayoutInflater.from(this).inflate(R.layout.actionbar, null, false);
actionbar.setCustomView(view);
}


I have found a recent post, that is pointing out that there is an issue with the latest release. I have also updated ADT and SDK to Android 5.

Android ActionBar's custom view not filling parent

I don't know what should I do.

Edit (partial solution):

Not working on Android <= API 10.

Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width

What have I changed:

Use the latest sdk version:

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />


Add a
toolbarStyle
:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/ActionBarStyle</item>
<item name="actionBarStyle">@style/ActionBarStyle</item>

<item name="android:toolbarStyle">@style/ToolbarStyle</item>
<item name="toolbarStyle">@style/ToolbarStyle</item>
</style>

<style name="ToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">
<item name="contentInsetStart">0dp</item>
<item name="android:contentInsetStart">0dp</item>
</style>

Answer

try this:

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    View customView = getLayoutInflater().inflate(R.layout.main_action_bar, null);
    actionBar.setCustomView(customView);
    Toolbar parent =(Toolbar) customView.getParent();
    parent.setContentInsetsAbsolute(0,0);

I used this code in my project,good luck;