Gunnar Bernstein
Gunnar Bernstein

Reputation: 6222

Android Button semi transparent on Jellybean but not on Lollipop

I have the latest Android Studio and SDK (Compile SDK 21, Build Tools 21.1.2, appcompat v7 21.0.3) and make a brand new wizard application with the blank activity template.

I then change only XML: The background of the root relative layout (android:background="#00FF00") and add a button (using graphic editor, no change to button).

This works fine on Lollipop devices. On a jellybean (4.2.2) device or emulator, the button renders in gray with a green touch.

This may be a topic with the theme (Theme.AppCompat.Light.DarkActionBar), but should it not render identically on Lollipop and Jellybean devices?

I have seen the questions about manually changing the background of the button (e.g. How to make button non transparent), this is a compatibility question.

Here is the full xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#00FF00">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="26dp"
    android:layout_marginStart="26dp"
    android:layout_marginTop="55dp" />

 </RelativeLayout>

Upvotes: 0

Views: 564

Answers (2)

Matt Accola
Matt Accola

Reputation: 4142

AppCompat does not currently support Button widgets so buttons on the Holo theme (API Levels less than 21) render differently than on the Material theme (API Levels 21+). You'll need to define a Button style like this:

<style name="MyButtonStyle" parent="Base.MyButtonStyle"/>

Then in the res/values folder you define the Base.MyButtonStyle

<style name="Base.MyButtonStyle" parent="android:Widget.Holo.Button">
    <item name="android:background">@drawable/btn_default</item>
</style>

The btn_default drawable would point to your custom selector for the button.

In the res/values-v21 you define Base.MyButtonStyle like this:

<style name="Base.MyButtonStyle" parent="android:Widget.Material.Button"/>

If you want to change the normal or pressed colors of the button for 21+ you would set colorButtonNormal and colorControlHighlight in the theme, respectively.

Upvotes: 1

Daniel Veihelmann
Daniel Veihelmann

Reputation: 1487

If you don't specify a style or add android:background="yourBackground" to the xml of your button, Android will render your button with the default button style of the Android version your app is running on.

For example, if you add android:background="#FF0000" to your button, there shouln't be a (big) difference on different android versions anymore.

Upvotes: 0

Related Questions