Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView Padding with a drawable background in xml

So... a known Android issue is that setting a drawable background to a TextView in conjunction with setting the padding (e.g. paddingLeft) will only work if the background is set before the padding.
So far so good (and annoying).

But, what if I want to do it via .xml (and not programmatically)? :(
Any ideas?

Here is what I want:
left padding should be bigger than drawable-to-text padding

But no matter what paddingLeft I set, nothing changes.
I'm positive it should work, but maybe it's a layout bug?

Here is the (very simplified) layout:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/call"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@drawable/call_btn"
    android:paddingLeft="@dimen/DESIRED_LEFT_PADDING"
    android:paddingStart="@dimen/DESIRED_LEFT_PADDING"
    android:drawableLeft="@drawable/ic_phone"
    android:drawablePadding="@dimen/drawable_padding"
    android:drawableStart="@drawable/ic_phone"
    android:singleLine="true" />

<Button
    android:id="@+id/address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/call"
    android:paddingLeft="@dimen/rest_item_btn_side_margin"
    android:paddingStart="@dimen/rest_item_btn_side_margin"
    android:background="@drawable/navigate_btn"
    android:drawableLeft="@drawable/ic_address"
    android:drawablePadding="@dimen/drawable_padding"
    android:drawableStart="@drawable/ic_address"
    android:gravity="start|center_vertical"
    android:singleLine="true" />  
</RelativeLayout>  

And this layout lies within another one, using the <include> tag:

<CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/appbar"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:background="@android:color/transparent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@android:color/transparent" >

        <include
            android:id="@+id/btn_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            layout="@layout/btn_layout" />
    </RelativeLayout>
</ScrollView>
</CoordinatorLayout>

Solution
Well, that is awkward, but I just had a global Button style setting paddingLeft... I was sure an xml declaration overrides style... Apparently not always. Is it a bug or shouldn't I trust this behavior?
Anyway, issue finally solved:)

like image 882
guy_m Avatar asked Dec 05 '25 03:12

guy_m


1 Answers

try this..if this matches your requirement :

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:gravity="center"
        android:padding="10dip"
        android:drawablePadding="10dip"
        android:drawableLeft="@drawable/ic_launcher"
        android:background="@drawable/preview_dailog_bg"
        android:text="swipe_element"
        android:textColor="@android:color/white"
        android:layout_centerInParent="true"/>
like image 150
Meenal Avatar answered Dec 08 '25 03:12

Meenal