Reputation: 491
I've got a button and a drawable on the left of the text, but I want the drawable to be closer to the text. So I need to move the drawable.
I've defined android:drawableLeft but the content of the button is not centered.
Here is my code:
<Button
android:id="@+id/addsubject"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_action_add"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="@string/addsubject"
android:textColor="@color/white"
android:background="@drawable/custombutton1" />
Here is how it looks now:
And here is how I'd like it to be:
Thank you!
Upvotes: 28
Views: 34710
Reputation: 364988
Use the app:iconGravity="textStart"
attribute in the MaterialButton
<com.google.android.material.button.MaterialButton
app:icon="@drawable/ic_add_24px"
app:iconGravity="textStart"
../>
If you want to reduce the padding between the icon and the text just use the app:iconPadding
attribute:
<com.google.android.material.button.MaterialButton
app:icon="@drawable/ic_add_24px"
app:iconGravity="textStart"
app:iconPadding="0dp"/>
Upvotes: 20
Reputation: 3963
for Materialbutton, you have to use the icon attribute and you can also change its tint with iconTint like this example:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon="@drawable/ic_cancel"
app:iconTint="@color/black"
android:text="text" />
Upvotes: 0
Reputation: 1268
There's also other possibilities that you want to inflate the drawable/icon programatically, after reading a while and using a lot of combination using XMLs, but still doesn't works,
You might want to fork below library instead, to suits your use-cases, such as, customizing the view, position, etc programatically, see below reference:
P.S: Copyrighted and regards to the author
Upvotes: 0
Reputation: 31
I done this simply using paddingLeft and paddingRight. Its works!
...
android:paddingLeft="25dp"
android:paddingRight="25dp"
/>
Upvotes: 3
Reputation: 741
I got this result by simply putting these properties to the button specifically
<Button
...
android:paddingLeft="60dp"
android:drawablePadding="-50dp"
android:gravity="center"
android:drawableLeft="@drawable/your_button_icon"
/>
You can tune in with your own values for your desired results.
Cheers :-)
Upvotes: 6
Reputation: 20426
The padding between the left border of button and right border of the icon is controlled by android:paddingLeft
property. The padding between the icon and the text is defined by android:drawablePadding
property.
Add these two properties to you button and set the values you are happy with.
<Button
...
android:paddingLeft="24dp"
android:drawablePadding="8dp"
/>
Upvotes: 1
Reputation: 482
You can use LinearLayout as your button by adding OnClickListener and customize however you want.
<LinearLayout
android:layout_width="160dip"
android:layout_height="60dip"
android:orientation="horizontal"
android:background="@drawable/custombutton1">
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_action_add"
android:layout_gravity="center"
android:layout_weight="1"
android:scaleType="center">
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addsubject"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
Upvotes: 1
Reputation: 1049
try this
<Button
android:id="@+id/addsubject"
android:layout_width="160dip"
android:layout_height="60dip"
android:layout_gravity="center"
android:drawableLeft="@drawable/ic_action_add"
android:drawablePadding="2dip"
android:gravity="center"
android:paddingLeft="30dip"
android:paddingRight="26dip"
android:singleLine="true"
android:text="@string/addsubject"
android:textSize="13dip" />
Upvotes: 19