Is it possible to set the margin or padding for the image which we added with the android:drawableLeft
?
textView.setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(this,drawable),null,null,null);
addressTitleView.setCompoundDrawablePadding();
I'll throw my answer into the ring as well. If you want to do this programmatically you can do the following.
final Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.somedrawable);
final boolean isLTR = ViewCompat.LAYOUT_DIRECTION_LTR == ViewCompat.getLayoutDirection(this);
final int iconInsetPadding = getResources().getDimensionPixelSize(R.dimen.icon_padding);
final Drawable insetDrawable = new InsetDrawable(drawable, isLTR ? 0 : iconInsetPadding, 0, isLTR ? iconInsetPadding : 0, 0);
This will add the padding to the end of the drawable where end will mean left/right depending if phone is in LTR or RTL.
You can use android:drawableLeft="@drawable/your_icon"
to set the drawable to be shown on the left side. In order to set a padding for the drawable you should use the android:paddingLeft
or android:paddingRight
to set the left/right padding respectively.
android:paddingRight="10dp"
android:paddingLeft="20dp"
android:drawableRight="@drawable/ic_app_manager"
Tries to use negative padding
Like:
android:paddingLeft="-8dp"
<TextView
android:layout_width="wrap_content"
android:layout_height="32dp"
android:background="@drawable/a"
android:drawableLeft="@drawable/concern_black"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:drawablePadding="10dp"
android:text="text"/>
note: layout_width needs to be wrap_content and use paddingLeft paddingRight drawablePadding to control gap. If you specify layout_width value is will has gap between icon and text, I think once give the layout_width a specify value, the padding will measure.
TextView has an android:drawablePadding property which should do the trick:
android:drawablePadding
The padding between the drawables and the text.
Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).
This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.
This corresponds to the global attribute resource symbol drawablePadding.