How to add an icon inside a button that forms an oval button
I created drawable button_oval.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="rectangle">
<corners android:radius="120dp" />
<solid android:color="#eceef5" />
<stroke
android:width="3dp"
android:color="#29395e" />
<size
android:width="300dp"
android:height="120dp" />
</shape>
and then i use it in my layout like this:
// I want to add a icon inside this button
<Button
android:id="@+id/goToPersonalPage"
android:layout_width="110dp"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:background="@drawable/button_oval"
android:text="@string/memberButton"
android:textColor="#29395e"
android:textSize="18dp" />
I want to add an icon inside Button
, can I complete it by changing mine button_oval.xml
?
Also like the image below.
Any help would be greatly appreciated.
+3
徐博俊
source
to share
3 answers
You have to add these lines to your Button element -
android:drawableLeft="@mipmap/ic_launcher_round" // to set an icon
android:drawablePadding="10dp" // to set the padding of icon from text
android:paddingLeft="20dp" // to set the padding of the icon and text
adjust the values according to your needs.
It will look like
+3
Deepanshu harbola
source
to share
Add this code to your Button
element
android:drawableLeft="@android:drawable/yourIcon"
+2
John joe
source
to share
in XML
android:drawableLeft="@drawable/button_icon"
android:drawablePadding="2dip"
or in class Acitvity
yourButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
yourButton.setCompoundDrawablePadding(padding_value);
+1
Faxriddin Abdullayev
source
to share