1

I am using Android Studio 2.2.1 with project with these settings:

compileSdkVersion 24
buildToolsVersion "24.0.2"
minSdkVersion 16
targetSdkVersion 24

If I use the GUI to change the button background, it adds this to the layout:

android:background="@color/colorPrimary"

Then, if I run the app on a 4.4 virtual device (Microsoft's Android Emulator because I'm on an AMD system and I want a fast emulator), or on a Samsung Galaxy S6 with Android 6.0.1, the button has the correct color but loses left and right padding and the text runs right to the left and right edge of the button.

If I set only the backgroundTint, then the button has the correct padding on the virtual device, but not the correct color. However, on the S6, it has the correct color and padding.

This seems like a bug somewhere, but where? Is it in the code generation or is this a bug in Android 4.4?

I think Android Studio should be doing whatever needed to make it work correct on both platform levels, whether it is as complex as some of these solutions:

Standard Android Button with a different color

or something more succinct.

My styles.xml file shows:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

and my AndroidManifest theme setting is:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
Community
  • 1
  • 1
Mark
  • 1,124
  • 1
  • 14
  • 21

1 Answers1

1

This seems like a bug somewhere, but where?

Right here:

android:background="@color/colorPrimary"

Is it in the code generation or is this a bug in Android 4.4?

No, it is your replacement background. It is a color, without any intrinsic padding, and without being wrapped in a StateListDrawable.

The stock background of Button widgets has some amount of padding enforced as part of the background itself (e.g., via some transparent pixels in the nine-patch PNG used for the background). The actual background itself is a StateListDrawable, which chooses one of several other drawable resources to apply based on the state of the Button (e.g., normal, pressed, focused, disabled).

To replace a Button background, you need to first use a StateListDrawable of your own. Otherwise, your Button will not appear to respond visually to click events or other state changes (e.g., being disabled). Then, you can either incorporate some padding into the backgrounds for your states, or put padding on the Button widget itself, as you see fit.

I think Android Studio should be doing whatever needed to make it work correct on both platform levels

Android Studio assumes that you know what you are doing. There is no hard-and-fast requirement that Button backgrounds have this sort of padding, and there is no hard-and-fast requirement that a Button be something that makes sense to users (versus "hey, why does this button seem to not respond visually when I tap on it?"). There will be scenarios where developers do want Button widgets to have no padding, such as in an implementation of a segmented-list-control sort of compound widget.

Personally, I think the decision to have some intrinsic padding in the Button background is regrettable. But, that's the way it was implemented back in Android 1.0, and Google has elected to maintain the approach, even with newer themes, presumably for backwards compatibility.

If I set only the backgroundTint, then the button has the correct padding on the virtual device, but not the correct color

I have not played with backgroundTint with appcompat-v7. It is possible that you are seeing a bug there. You might consider posting a separate question with a complete example, plus screenshots, to get more specific help with that particular concern.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • BTW, I filed [a feature request](http://code.google.com/p/android/issues/detail?id=224980) to have Lint warn you when applying a color as a background to a `Button`. Admittedly, this would not directly address your "where'd my padding go?" concern, but it would help to point out that `Button` backgrounds are not simple colors normally. – CommonsWare Oct 11 '16 at 15:45
  • I found: http://stackoverflow.com/questions/32335232/button-backgroundtint-not-working-with-appcompat-v23 and changing it to: android.support.v7.widget.AppCompatButton with: app:backgroundTint="@color/colorPrimary" everything then works correctly. – Mark Oct 11 '16 at 16:13
  • I just ran across this too: https://code.google.com/p/android/issues/detail?id=203872 so this may not always work I guess? – Mark Oct 11 '16 at 16:26
  • @Mark: Possibly. As I wrote, I have not played with `backgroundTint` with `appcompat-v7`. My answer focuses on the problem with setting the background directly as a result. – CommonsWare Oct 11 '16 at 16:40