Search code examples
androidandroid-actionbarandroidxandroid-actionbar-compat

Set app:contentInsetStartWithNavigation on ActionBar programmatically


I have upgraded an older Android app to androidx.

Now there is a big space before the icon in the Actionbar.

enter image description here

I was looking into it and found a question where it explains that it is due to

app:contentInsetStartWithNavigation

and

app:contentInsetStart

Is there a way to set them both to 0dp programmatically?

My code at the moment:

public static void initializeActionBar(Context context, ActionBar bar, boolean showBackButton){
    if (bar != null) {
        bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
        bar.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.wood2));
        bar.setIcon(R.drawable.icon_white); //custom logo
        if(showBackButton){
            bar.setDisplayHomeAsUpEnabled(true);
        }
    }
}

This code is used in various activities like this:

ActivityTools.initializeActionBar(this,getSupportActionBar(), false);

Solution

  • You need to use Toolbar for this

    First, use a Toolbar

    then you can use setContentInsetStartWithNavigation(int insetStartWithNavigation)

    SAMPLE CODE

    class DemoActivity : AppCompatActivity() {
    
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_recurring_billing)
    
            setSupportActionBar(mToolbar)
    
            mToolbar!!.setContentInsetsAbsolute(10, 0)
            mToolbar!!.contentInsetStartWithNavigation =10
    
    
    
        }
    }