Setting default background of ImageButton to transparent (using AndroidX)

Viewed 535

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?

3 Answers

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

Add this line in your code to

style="@style/MyImageButton"

So,

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

or you don't want to add style in your all button tag you can follow this answer to change the style of the default button. Hope it will help you. Try and let me know it works or not.

Thanks. Happy coding :)

You can simply set background of ImageButton to null as below :

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

This will make your ImageButton transparent.

Related