Is it possible to set the margin or padding for the image which we added with the android:drawableLeft
?
Instead of Button
use LinearLayout
with ImageView
and TextView
inside. In child items like ImageView
and TextView
use android:duplicateParentState="true"
.
define a shape for your edittext and give it a padding For Example
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<padding
android:left="5dp"
android:right="5dp"
/>
<solid android:color="#F6F6F6" />
<stroke
android:width="1px"
android:color="#C3C3C3" />
<corners
android:bottomLeftRadius="1dp"
android:bottomRightRadius="1dp"
android:topLeftRadius="1dp"
android:topRightRadius="1dp" />
</shape>
The padding defined in this shape will help in give padding to drawableleft or right ---------------------- Apply this shape on EditView
<EditText
android:id="@+id/example"
android:layout_width="fill_parent"
android:layout_height="36dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/shape2"
android:drawableLeft="@drawable/icon1"
android:drawablePadding="@dimen/txtDrwblPadding"
android:ems="10"
/>
using that defined shape as background will give your EditText some style plus margin to drawableLeft.
android:drawablePadding
is the easiest way to give padding to drawable icon but You can not give specific one side padding like paddingRight
or paddingLeft
of drawable icon.To achieve that you have to dig into it.
And If you apply paddingLeft
or paddingRight
to Edittext
then it will place padding to entire Edittext
along with drawable icon.
android:drawablePadding is the easiest way to give padding to drawable icon but You can not give specific one side padding like paddingRight or paddingLeft of drawable icon.To achieve that you have to dig into it. And If you apply paddingLeft or paddingRight to Edittext then it will place padding to entire Edittext along with drawable icon.
<TextView android:layout_width="match_parent"
android:padding="5sp"
android:id="@+id/date"
android:gravity="center|start"
android:drawableEnd="@drawable/ic_calendar"
android:background="@drawable/edit_background"
android:hint="Not Selected"
android:drawablePadding="10sp"
android:paddingStart="10sp"
android:paddingEnd="10sp"
android:textColor="@color/black"
android:layout_height="wrap_content"/>
You can use a padding for the button and you can play with drawablePadding
<Button
style="@style/botonesMenu"
android:padding="15dp"
android:drawablePadding="-15dp"
android:text="@string/actualizarBD"
android:textAlignment="gravity"
android:gravity="center"
android:layout_row="1"
android:layout_column="0"
android:drawableTop="@drawable/actualizar"
android:id="@+id/btnActualizar"
android:onClick="actualizarBD" />
you can use a specific padding depends where put your drawable, with android:paddingLeft="10dp" or android:paddingBottom="10dp" or android:paddingRight="10dp" or android:paddingTop="10dp"
If the size of drawable resouce is fixed, you can do like this:
<Button
android:background="@drawable/rounded_button_green"
style="?android:attr/selectableItemBackground"
android:layout_height="wrap_content"
app:layout_widthPercent="70%"
android:drawableRight="@drawable/ic_clear_black_24dp"
android:paddingRight="10dp"
android:paddingLeft="34dp"
tools:text="example" />
The key here is that:
android:drawableRight="@drawable/ic_clear_black_24dp"
android:paddingRight="10dp"
android:paddingLeft="34dp"
That is, the size of drawable resource plus paddingRight is the paddingLeft.
You can see the result in this example