Search code examples
androidandroid-stylesandroidxandroid-imagebutton

Setting default background of ImageButton to transparent (using AndroidX)


I'm using the AndroidX support libraries. In my manifest I declare:

<application
        ...            

        android:theme="@style/AppTheme">

In values.styles.xml I declare:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...

    <item name="android:imageButtonStyle">@style/MyImageButton</item>
</style>

<style name="MyImageButton" parent="Widget.AppCompat.ImageButton">
    <item name="android:background">@android:color/transparent</item>
</style>

Then I use the ImageButton via:

<ImageButton
    android:id="@+id/image"
    android:layout_width="40dp"
    android:layout_height="40dp" />

Unfortunately, my ImageButton shows a grey background instead of the transparent one. What do I have to do to fix it?


Solution

  • You need to replace this line

    <item name="android:imageButtonStyle">@style/MyImageButton</item>
    

    with

    <item name="imageButtonStyle">@style/MyImageButton</item>
    

    And This is also another way given by @prince