A Poor Default (Or: Why Your Button Does Not Work)

Android Studio 4.1’s new-project wizard creates projects that include the Material Components for Android library by default, and sets up the default theme to use Theme.MaterialComponents.DayNight.DarkActionBar as a base, at least when using many of the templates.

For an experienced developer, this is a reasonable choice. However, a new-project wizard should not be aimed at experienced developers, as experienced developers probably already have their project. A new-project wizard should be aimed at newcomers, because their very first experience with Android app development will come from the project that the wizard creates.

And for newcomers, Material Components for Android is a dreadful choice, in part because newcomers have no idea that this library exists, what it is for, or how it impacts app development.

For example, suppose you are a brand-new Android app developer. You install Android Studio 4.1 and use the new-project wizard. You add a <Button> to your activity_main layout resource, and you want to change its background.

At this point, you’re already screwed, and you are only a few minutes into your first attempt at app development.

Suppose you decide that reading the documentation is the thing to do.

You go to the documentation for Button, and you see a helpful paragraph on just your topic of concern:

Every button is styled using the system’s default button background, which is often different from one version of the platform to another. If you are not satisfied with the default button style, you can customize it. For more details and code samples, see the Styling Your Button guide.

So you click that “Styling Your Button” link and proceed to read about how you might consider setting your theme to Theme.Holo, because that page has not been updated in six years or so.

But, it goes on to say:

To customize individual buttons with a different background, specify the android:background attribute with a drawable or color resource.

Fantastic! So you set up a color for your android:background, or maybe even a <shape> drawable resource, as this developer did… and you find that your background does not take effect.

Why? Because Material Components for Android does not work that way.

If you happen to know where the Material Components for Android documentation for Button resides, you will stumble upon:

Note: <Button> is auto-inflated as <com.google.android.material.button.MaterialButton> via MaterialComponentsViewInflater when using a non-Bridge Theme.MaterialComponents.* theme.

Even experienced developers might have a difficult time interpreting that.

But that might lead you to this page on MaterialButton, where it says:

Do not use the android:background attribute

So, the documentation says to use android:background, and the documentation says not to use android:background. Great.

The MaterialButton documentation does not do a particularly good job of explaining what the alternative is (hint: use android:backgroundTint or adjust colorPrimary in your theme to change the color).

So, we have to wander to multiple sites and deal with conflicting documentation, just to set a background color on a button.

AppCompat is far from perfect. But, it at least works fairly conventionally. Yes, inflater magic means that <Button> turns into an AppCompatButton, but AppCompatButton tends to behave like a regular Button, at least in terms of things like setting the background color. Newcomers to Android, reading the documentation or Medium posts or Stack Overflow, are more likely to succeed in what they are trying to do with AppCompat than with the Material Components for Android.

Moreover, AppCompat has been the official Google recommended choice for quite a few years now. Quite a bit has been written specifically for AppCompat. So, answers to questions like “how do I BLEEP-ing well set a background color on a button?” are likely to exist. Far fewer of those answers will exist for Material Components for Android… and that even assume the newcomer has any idea that Material Components for Android has something to do with their project.

This is not to hate on Material Components for Android as a library. It is a perfectly cromulent set of widgets for experienced developers. IMHO, it is a poor choice to inflict upon newcomers, and that’s why I think it is a poor choice as the default for Android Studio’s new-project wizard.

Interested in Jetpack Compose? jetc.dev has a weekly newsletter of the latest articles, samples, and other details of Compose development!