0

I have used setDisplayHomeAsUpEnabled(true) to display the home up icon. That works fine, but it takes a lot of margin which I am trying to reset by using the below code -

ImageView view = findViewById(android.R.id.home);
view.setPadding(7,0,0,0);

But it returns null pointer exception.

Code to enable the home up icon -

final LayoutInflater actionBarInflater = (LayoutInflater) 
    this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewActionBar = actionBarInflater.inflate(R.layout.action_bar, null);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

Image shows the home up icon and my app icon, i want to reduce the space between theme

Any help is appreciated. Thank you!

Azaz Sayed
  • 53
  • 7
  • https://stackoverflow.com/questions/9685658/add-padding-on-view-programmatically/9685690#9685690 – Hafiza Apr 27 '20 at 12:14
  • @hafiza I am not sure if I am missing something, but the two look unrelated. – Azaz Sayed Apr 27 '20 at 12:27
  • That ID is not a `View` ID, which is why `findViewById()` won't find it. It's the ID for a `MenuItem` that that particular button can act as, if set up appropriately. It's probably not worth the hassle it would take to get a reference to that button in code. You should be able to adjust that gap with an XML attribute. I believe that would be `contentInsetStartWithNavigation`, like is mentioned in [this answer](https://stackoverflow.com/a/40717794). – Mike M. Apr 27 '20 at 13:09
  • Thanks Mike, it helped, but some space still remains, unlike WhatsApp. I think what WhatsApp has done is use some custome toolbar and that explains why even if you click on the image next to the up icon, it takes you back instead of showing friend's profile, and you actually need to click on friend/group name to view the details. Thanks for quick help. – Azaz Sayed Apr 28 '20 at 01:01
  • Well, the only other spacing there would be coming from the width of that button, as it's wider than the icon. If you want to play around with that, you could use [my answer here](https://stackoverflow.com/a/46495771) as a guide. It shows how to style that button. In this case, you would want an `??dp` instead of the `tint` one that's there. The default value for the is `56dp`, I believe, so you could definitely make it thinner. As you noted, though, a fully custom `Toolbar` might be easier to get behaving as needed. – Mike M. Apr 29 '20 at 00:47
  • Btw, you'll need to use @Mike in order for me to be notified of your comments, if you have any more questions, since there's another user here. – Mike M. Apr 29 '20 at 00:48
  • @Mike thanks once again. You can tell I will be posting many more question in the coming days, hope you will be around. :) – Azaz Sayed Apr 29 '20 at 04:35
  • @Mike For now I will just go ahead with your previous suggestion and allow some space in between, otherwise it will not be user friendly to click on the up icon, user may end up clicking on the image icon instead. – Azaz Sayed Apr 29 '20 at 04:42

1 Answers1

0

To access home button see my answer. But it would not help you to remove redundant paddings and margins from it. For that you should reset it in your toolbar layout xml:

     <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/actionbar_size"
            android:contentInsetLeft="0dp"
            android:contentInsetStart="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            android:contentInsetRight="0dp"
            android:contentInsetEnd="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetEnd="0dp"
            app:contentInsetStartWithNavigation="0dp"
            android:padding="0px"
            android:clipToPadding="false">
Oleksandr Albul
  • 1,611
  • 1
  • 23
  • 31