5
votes

I've been pulling my hairs against this left padding/margin just don't go away issue, about to become bald... A solution to this would be much appreciated! I've tried these answers from SO, it did moved some of the padding, but not all of them. Basically all the answers says to set the contentInsetStart and contentInsetLeft to 0dp. This does remove some of the padding on the left, but I still see a padding, it's just smaller than before I set the contentInsetStart and contentInsetLeft to 0dp. This solution works on Samsung phones, but not on any tablets I've tested, such as Nexus 7, Nexus 9, Dell Venus 8, etc. enter image description here

I believe a lot of people had this issue and these are the answers I've tried but still leaving a padding/margin on the left shown above.

Android: remove left margin from actionbar's custom layout

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

Android API 21 Toolbar Padding

Padding/margin on the left side of my activity/actionbar - dont know where it comes from, cannot remove it

How to remove default layout's padding/margin

How to remove the margin between the app icon and the edge of the screen on the ActionBar?

The style

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/AppThemeActionBar</item>
        <item name="logo">@android:color/transparent</item>
    </style>

    <style name="AppThemeActionBar" parent="@style/Widget.AppCompat.ActionBar">
        <item name="displayOptions">showCustom</item>
        <item name="background">@color/blue</item>
        <item name="actionBarSize">50dp</item>
        <item name="height">50dp</item>

        <item name="contentInsetLeft">0dp</item>
        <item name="contentInsetStart">0dp</item>
        <item name="contentInsetEnd">0dp</item>
        <item name="android:layout_marginLeft">0dp</item>
    </style>
</resources>

The activity layout, activity_main.xml, it's pretty much empty, just a dummy layout for the Main activity class, the custom action bar view is in another layout file.

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

</LinearLayout>

Now, here is the custom actionbar layout

<?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="50dp" >

    <Button
        android:id="@+id/btn_home"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="#ea6464"
        android:text="Home"/>

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn_home"
        android:gravity="center"
        android:textColor="#fff"
        android:textStyle="bold"
        android:text="Hello"/>

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@android:drawable/ic_menu_edit"
        android:background="@null"/>
</RelativeLayout>

The MainActivity.java which inflates the custom actionbar layout and set it mActionBar.setCustomView(mCustomView);

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar mActionBar = getSupportActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);

        LayoutInflater mInflater = LayoutInflater.from(this);
        View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);

        Button btnHome = (Button) findViewById(R.id.btn_home);

        btnHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Home Button Clicked!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

The sample app with this issue can be found here

2
i think that you should use toolbar instead of actionbarNorutan
I've tried with Toolbar and it gives the same issue.s-hunter
so if toolbar or actionbar did not act what you want, you can create a layout like toolbar (can use RelativeLayout) and use as a toolbarNorutan
Yes, that will be my last resort but I would like to know if there is a solution to do it with actionbar or toolbar or it's just not possible.s-hunter
I encountered the same problem on Android M (only on tablet). @s-hunter did you find any solution?Samik

2 Answers

1
votes

You should use a ToolBar instead of an ActionBar and call setContentInsetsAbsolute or if you really want to keep ActionBar, you should do it like that :

ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);

LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
Toolbar parent = (Toolbar) customView.getParent();
parent.setContentInsetsAbsolute(0,0);
0
votes

Haven't tried the Toolbar setContentInsetsAbsolute function because it only works on API 21 or higher. Minimum target of my app is API 15 .

User Nilesh Senta's solution on this same (extra margin issue) was to update the style and it worked for me! Below is my code

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarStyle">@style/Actionbar</item>
    <item name="android:titleTextStyle">@style/ActionbarTitle</item>
</style>

<style name="Actionbar" parent="Widget.AppCompat.ActionBar">
    <item name="android:titleTextStyle">@style/ActionbarTitle</item>
    <item name="contentInsetStart">0dp</item>
    <item name="contentInsetEnd">0dp</item>
</style>

java (onCreate)

ActionBar actionBar =  getSupportActionBar();

actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);

ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT
    );

View view = LayoutInflater.from(this).inflate(R.layout.actionbar_main, null);

actionBar.setCustomView(view, layoutParams);