Button background as transparent
I have a button. When I press the button I have to make text as bold otherwise normal. So I wrote styles for bold & normal.
<style name="textbold" parent="@android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">bold</item>
</style>
<style name="textregular" parent="@android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">normal</item>
</style>
Now I have a button_states.xml as:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
style="@style/textbold" />
<item android:state_focused="false" android:state_pressed="true"
style="@style/textregular" />
<item style="@style/textregular" />
</selector>
In my layout for this button, I have to make the background as transparent too...How will I do it? My layout code is :
<Button android:id="@+id/Btn" android:background="@drawable/button_states" />
How will I include background as transparent in my style?
Solution 1:
To make a background transparent, just do android:background="@android:color/transparent"
.
However, your problem seems to be a bit deeper, as you're using selectors in a really weird way. The way you're using it seems wrong, although if it actually works, you should be putting the background image in the style as an <item/>
.
Take a closer look at how styles are used in the Android source. While they don't change the text styling upon clicking buttons, there are a lot of good ideas on how to accomplish your goals there.
Solution 2:
Try new way to set background transparent
android:background="?android:attr/selectableItemBackground"
Solution 3:
You may also use: in your xml:
android:background="@null"
or in code:
buttonVariable.setBackgroundColor(Color.TRANSPARENT);
Solution 4:
use #0000
(only four zeros otherwise it will be considered as black
) this is the color code for transparent. You can use it directly but I recommend you to define a color in color.xml so you can enjoy re-usefullness of the code.