By Alvin Alexander. Last updated: June 6, 2016
As a quick Android “centering” tip, I found that by adding this snippet to the ImageButton
below:
android:layout_gravity="center"
I was able to get the ImageButton
to center itself in the Toolbar
. Here’s the layout code for the Toolbar
and the ImageButton
:
<android.support.v7.widget.Toolbar android:id="@+id/bottomToolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#ffffa302" android:minHeight="@android:dimen/notification_large_icon_height"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pinButton" android:layout_centerHorizontal="true" android:layout_gravity="center" android:drawable="@drawable/pinterest_pin" android:clickable="true"/> </android.support.v7.widget.Toolbar>
I don’t think I needed this line in the ImageButton:
android:layout_centerHorizontal="true"
but I could be wrong. Once I got this Toolbar floated down to the bottom of the display I decided I didn’t like the look of it after all, so I deleted this code before getting a screenshot. But I do know that the android:layout_gravity
setting was the one that centered my ImageButton. (Also, a lot of that other layout configuration code is unnecessary. I was experimenting with several different things when I was working on this button centering problem.)