Search code examples
androidandroid-actionbarandroid-homebutton

How to set Home-Icon margins when using setDisplayHomeAsUpEnabled(true)?


I have a problem with native actionbar (not ABS). In normal state icon has 26 px margins on both side.

enter image description here

But when I call setDisplayHomeAsUpEnabled(true) it decreases this margins and in result it looks much narrower.

enter image description here

Do you have any idea how to maintain this margins when setDisplayHomeAsUpEnabled(true) is called? (prefferably without workarounds and custom views)

Thanks is advance.


Solution

  • You can set the margin of the home-icon like that: (just call the below code inside your Activity's onCreate(...) method)

    ImageView ivIcon = (ImageView) findViewById(android.R.id.home);
    
    FrameLayout.LayoutParams lpIcon = (FrameLayout.LayoutParams) ivIcon.getLayoutParams();
    
    lpIcon.topMargin = lpIcon.bottomMargin = yourmargin;
    lpIcon.leftMargin = lpIcon.rightMargin = yourmargin;
    ivIcon.setLayoutParams(lpIcon);
    

    Its kind of a hack but I used it to get rid of the margin completely by setting it to 0.