4

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?

Christian
  • 25,249
  • 40
  • 134
  • 225

3 Answers3

5

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

Sunny
  • 3,134
  • 1
  • 17
  • 31
1

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 :)

Pranav P
  • 1,755
  • 19
  • 39
0

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.

Apurv
  • 391
  • 2
  • 9
  • 1
    This question is about getting rid of the work of doing that for every ImageButton via styles. Your answer doesn't use styles or otherwise sets the default. – Christian Jun 20 '19 at 09:28