Removing the background of a button Android Studio is one of the most common tasks when customizing the interface. Standard elements Material Design often have a specified background (ripple effect, shadow or color), which does not always fit into the application design. The problem is complicated by the fact that in different versions Android and Android Studio solutions may differ: what worked in API 21may not work in API 34.
Many beginners try to simply set android:background="@null" - and are faced with the fact that the button loses not only background, but also clickability, click animation, or even disappears completely. In this article we will examine background removal, taking into account all the nuances: from simple XML to programmatic configuration via 5 proven methods background removal taking into account all the nuances: from simple XML to software configuration via Kotlin/Java. We will pay special attention to maintaining the functionality of the button after changes.
If you work with Material Components (library com.google.android.material:material), the solutions will differ from the standard Button or AppCompatButton. We will look at both options, and also show how to avoid common mistakes, for example, the disappearance ripple effect or failure of the onClickListener.
1. Removing the background via XML: basic method
The easiest way to remove the background is to set an empty value for the attribute background directly in the markup activity_main.xml or other layout file. This method works for standard buttons (Button, AppCompatButton), but may not work for MaterialButton without additional manipulation.
Open your XML markup file and find the button tag. Add or change the line:
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press me"
android:background="@null" />
โ ๏ธ Attention: After using this method, the button may lose visual feedback when pressed (ripple effect). To return it, you will have to manually add foreground or use ?attr/selectableItemBackground:
android:foreground="?attr/selectableItemBackground"
If you are working with MaterialButton, simple @null will not be enough - you will need to disable the material style:
<com.google.android.material.button.MaterialButton
...
style="@style/Widget.MaterialComponents.Button.TextButton" />
To quickly find all the buttons in the project, use code search in Android Studio: click Ctrl+Shift+F (Windows/Linux) or Cmd+Shift+F (Mac) and enter <Button or MaterialButton.
2. Using styles (styles.xml) for global changes
If you need to remove the background from all buttons in the application, it is advisable to create a separate style in the file res/values/styles.xml. This will eliminate the need to write android:background="@null" for each button manually.
Add to styles.xml a new style:
<style name="TransparentButton" parent="Widget.AppCompat.Button"><item name="android:background">@null</item>
<item name="android:textColor">?attr/colorPrimary</item>
</style>
Now apply it to buttons through the attribute style:
<Buttonandroid:id="@+id/myButton"
style="@style/TransparentButton"
android:text="Transparent button" />
For MaterialButton use the parent style Widget.MaterialComponents.Button.TextButton:
<style name="TransparentMaterialButton" parent="Widget.MaterialComponents.Button.TextButton"><item name="backgroundTint">@null</item>
</style>
โ ๏ธ Attention: In some versions Material Components (especially before1.4.0) styles may conflict with attributesapp:backgroundTint. If the button is not displayed, try explicitly settingapp:backgroundTint="@null"in XML.
| Button type | Background attribute | Additional settings |
|---|---|---|
Button |
android:background="@null" |
Add android:foreground for ripple effect |
AppCompatButton |
android:background="@null" |
Use style with parent Widget.AppCompat.Button |
MaterialButton |
app:backgroundTint="@null" |
Set style Widget.MaterialComponents.Button.TextButton |
3. Programmatic background removal via Kotlin/Java
Sometimes the background needs to be removed dynamically - for example, when the application theme is changed or after data is loaded. In such cases, a software solution for Kotlin or Java.
For a standard button in Kotlin:
val myButton: Button = findViewById(R.id.myButton)
myButton.background = null
For MaterialButton will require additional configuration:
val materialButton: MaterialButton = findViewById(R.id.materialButton)materialButton.backgroundTintList = null
materialButton.setBackgroundColor(Color.TRANSPARENT)
In Java the code will look like this:
Button myButton = findViewById(R.id.myButton);myButton.setBackground(null);
MaterialButton materialButton = findViewById(R.id.materialButton);
materialButton.setBackgroundTintList(null);
materialButton.setBackgroundColor(Color.TRANSPARENT);
Why after setBackground(null) a button can become unclickable?
This happens because the standard button background includes not only the color, but also the click area (hit area). To restore clickability, add android:clickable="true" to XML or programmatically call setClickable(true).
โ ๏ธ Attention: When you programmatically remove the background in MaterialButton the click animation may disappear. To return it, use:
materialButton.setRippleColor(ColorStateList.valueOf(Color.GRAY))
4. Working with MaterialButton: features and pitfalls
MaterialButton from the library com.google.android.material:material behaves differently than standard buttons. Here the background is controlled not only through background, but also through the attributes backgroundTint, strokeColor, and rippleColor.
To completely remove the background, you will need:
- Disable
backgroundTint(main background color). - Set a transparent color via
backgroundColor. - If necessary, disable the stroke (
stroke).
XML example for a fully transparent MaterialButton:
<com.google.android.material.button.MaterialButtonandroid:id="@+id/transparentMaterialButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Transparent MaterialButton"
app:backgroundTint="@null"app:backgroundTintMode="multiply"
android:background="@android:color/transparent"
app:strokeColor="@android:color/transparent" />
Critical nuance: If you use MaterialButton with an icon (app:icon), a transparent background can make the icon invisible. In this case, you will have to explicitly set the color of the icon via app:iconTint.
Make sure that the button text is visible (specify android:textColor)
Check the clickability (click on the button)
Make sure that the ripple effect works (or add it manually)
If there is an icon, check its visibility (app:iconTint)
-->
5. Customization through themes and attributes
If your project uses Material Components Theme, you can globally customize the appearance of all buttons through theme files (themes.xml). This is especially convenient for supporting dark/light themes..
Add customization to themes.xml customization for MaterialButton:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight"><item name="materialButtonStyle">@style/Widget.MyApp.Button</item>
</style>
<style name="Widget.MyApp.Button" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">@null</item>
<item name="android:textColor">?attr/colorOnPrimary</item>
</style>
Now everything MaterialButton in the application will be transparent. default. To return the background for individual buttons, override the style locally:
<com.google.android.material.button.MaterialButton
... />
โ ๏ธ Attention: When using themes, pay attention to the colorOnPrimary attribute - it is responsible for the color of the text on the buttons. If the background is transparent and the text remains white (as in a dark theme), the button may become unreadable via android:textColor.
6. Alternative approaches: custom View and vectors
If standard methods do not give the desired result, consider alternative solutions:
- ๐จ Use
TextViewwith a click: Replace the button withTextViewc attributeandroid:clickable="true"and handlersetOnClickListener. This will give complete control over the appearance. - ๐ผ๏ธ Vector background images: If you need a translucent background, create a vector one
drawablewith the desired transparency and assign it viaandroid:background="@drawable/custom_bg". - ๐ง Custom
View: Create your own button implementation, inheriting fromAppCompatButtonorMaterialButton, and override the methodonDraw.
Example of a custom button based on TextView:
<TextViewandroid:id="@+id/customButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom button"
android:background="?attr/selectableItemBackground"
android:padding="16dp"
android:clickable="true"
android:focusable="true" />
In Kotlin add a handler:
findViewById(R.id.customButton).setOnClickListener { // Your code when clicked
}
Using a TextView instead of a Button is a universal way to create a custom button without a background, but do not forget to add the clickable and focusable attributes, otherwise clicks will not be processed.
FAQ: Frequently asked questions about removing the background of buttons
Why after android:background="@null" the button stopped responding on clicks?
This happens because the standard button background includes not only a visual element, but also a click area (hit area). Solutions:
- Add
android:clickable="true"to XML. - Use
android:foreground="?attr/selectableItemBackground"to save the ripple effect. - Set the minimum height/width of the button (
android:minHeight="48dp").
How to remove the background of a button without losing the animation? ConstraintLayout without losing animation?
Q ConstraintLayout the problem is often related to an attribute conflict. Try:
<Button...
android:background="@null"
app:backgroundTint="@null"
android:foreground="?attr/selectableItemBackground" />
If the animation still disappears, wrap the button in MaterialCardView with transparent background:
<com.google.android.material.card.MaterialCardViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/transparent"
app:rippleColor="?attr/colorPrimary">
<Button android:background="@null" />
</com.google.android.material.card.MaterialCardView>
Is it possible to remove the background of a button in Android Studio via Preview?
No, Preview in Android Studio does not allow you to directly edit attributes buttons. However, you can:
- Use Design Editor for visual editing (right click on the button โ
Edit Background). - Enable
Tools:background="@null"for preview (does not affect the real application).
Example:
<Button
...
android:background="@null"
tools:background="@android:color/holo_red_light" />
This will allow you to see a button with a red background in Preview, but in the application it will be transparent.
How to make a button transparent, but keep the shadow?
The shadow (elevation) and the background are controlled by different attributes. For MaterialButton use:
<com.google.android.material.button.MaterialButton
...
android:background="@android:color/transparent"
app:backgroundTint="@null"
app:elevation="4dp" />
For standard buttons, wrap it in CardView:
<androidx.cardview.widget.CardViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="4dp"
app:cardBackgroundColor="@android:color/transparent">
<Button android:background="@null" />
</androidx.cardview.widget.CardView>
Why is a Android 12+ button with a transparent background displayed with a gray background?
Starting c Android 12 (API 31), the system automatically adds a background (scrim) to improve readability. To disable it, add to the topic:
<style name="Theme.MyApp" parent="Theme.Material3.DayNight.NoActionBar"><item name="android:enforceMaterialTheme">false</item>
</style>
Or explicitly disable it for the button:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {button.setHasOverlappingRendering(false)
}