Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the left padding margin extra space in the ActionBar in Android?

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

like image 910
s-hunter Avatar asked Nov 09 '22 17:11

s-hunter


1 Answers

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);
like image 138
Andros Avatar answered Nov 14 '22 23:11

Andros