1

As the question states, I'm running into some weird behavior where my left drawables are not given any left padding on an HTC One running Android 4.4.

The layout where I'm seeing this is

<LinearLayout>

   <include layout="@layout/search_bar" />

   <!-- Some Fragment layout information -->

</LinearLayout>

Inside search_bar I have a Layout which looks like.

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="56dp"
        style="@style/SearchBox.SingleSearchBar" />
 </FrameLayout>

The SearchBox.SingleSearchBar style looks like (after some massaging of the style hierarchy).

 <style name ="SearchBox.SingleSearchBar" parent="@android:style/Widget.TextView">
    <item name="android:focusable">false</item>
    <item name="android:gravity">center_vertical</item>
    <item name="android:singleLine">true</item>
    <item name="android:lines">1</item>
    <item name="android:imeOptions">actionSearch|flagNoExtractUi</item>
    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:selectAllOnFocus">true</item>
    <item name="android:drawableLeft">@drawable/gray_magnifying_glass</item>
    <item name="android:scrollHorizontally">true</item>
    <item name="android:paddingLeft">@dimen/search_drawable_padding</item>
    <item name="android:paddingRight">@dimen/search_drawable_padding</item>
    <item name="android:paddingTop">@dimen/search_drawable_padding</item>
    <item name="android:paddingBottom">@dimen/search_drawable_padding</item>
    <item name="android:drawablePadding">@dimen/search_drawable_padding</item>
    <item name="android:maxLines">1</item>
    <item name="android:background">@drawable/search_bar_selector</item>
    <item name="android:textAppearance">@style/TextAppearanceSearchBox</item>
    <item name="android:textCursorDrawable">@drawable/search_box_cursor</item>
</style>

On every device that I've seen except for the HTC One, this runs fine: the left drawable is padded in search_drawable_padding dips from the left, and there is appropriate padding between the drawable and the text. However, for some reason, on the HTC One, there is no padding between the left edge of the text box and the drawable.

I logged the value for both getPaddingLeft() and getPaddingTop and found that on the HTC One they are 0px, while on a Samsung GS4 they are 48 pixels, which makes sense given that search_drawable_padding is 16dips.

Is there any way around this? Am I missing something really stupid that is forcing me to call setPadding in code in order to make this work?

Jonathan
  • 3,369
  • 4
  • 22
  • 27
  • I would suggest you to try it on an another HTC One device. Many times it happens that the piece of code don't work for particular device only & it's not the model issue. – Rohit Chaskar Feb 18 '15 at 02:52

1 Answers1

0

Please check your background drawable search_bar_selector,

<item name="android:background">@drawable/search_bar_selector</item>

Setting the background can reset the value of padding to 0 (see here).

One solution to fix this is to set the padding value again in file search_bar_selector, (Good solution, since you need not to set padding value programmatically).

<padding android:bottom="@dimen/search_drawable_padding" android:left="@dimen/search_drawable_padding" android:right="@dimen/search_drawable_padding" android:top="@dimen/search_drawable_padding" />
Community
  • 1
  • 1
impulse
  • 206
  • 2
  • 4