Button background as transparent


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?





So many of these good answers could be accepted by now...
– QED
Aug 30 '15 at 1:11






check out this answer here to make transparent button with border
– Basheer AL-MOMANI
Mar 31 '16 at 22:10





' why don't you just use a TextView and call the action on onClick of it ?
– Dula wnRp
May 7 '16 at 13:05





13 Answers
13



To make a background transparent, just do android:background="@android:color/transparent".


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/>.


<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.





I m already giving android:background as the xml..
– Mathew
Feb 10 '11 at 6:59





Take a look at my clarified answer. I misread your problem to start. The background gets set in the style.
– Steve Pomeroy
Feb 10 '11 at 7:03





+1 Great answer! Thank you for your help!
– BlackHatSamurai
Aug 10 '12 at 0:40





Sorry, I'm not clear on the solution. Just setting @android:color/transparent doesn't support the on-touch styles that normal buttons have (and is equivalent to setting the background to @null).
– gatoatigrado
Oct 14 '12 at 21:07





@gatoatigrado: do you mean that you want a transparent area to be outlined in orange or shaded in some what? If so, you'll need to create custom 9-patch images for that.
– Steve Pomeroy
Oct 15 '12 at 15:28



Try new way to set background transparent


android:background="?android:attr/selectableItemBackground"





This is what I would recommend using. This way, you get to keep the beautiful material design animations for your buttons =)
– Ryan Newsom
Feb 4 '16 at 16:59



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.


#0000


black





Luckily this has already been defined for us as @android:color/transparent
– Richard Le Mesurier
Jan 12 '16 at 8:34


@android:color/transparent



Add this in your Xml - android:background="@android:color/transparent"


android:background="@android:color/transparent"


<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button"
android:background="@android:color/transparent"
android:textStyle="bold"/>



You may also use:
in your xml:


android:background="@null"



or in code:


buttonVariable.setBackgroundColor(Color.TRANSPARENT);





Upvote for giving alternatives, love to see all the options!
– Burkely91
Jul 10 '16 at 11:55



I used


btn.setBackgroundColor(Color.TRANSPARENT);



and


android:background="@android:color/transparent"



Selectors work only for drawables, not styles. Reference



First, to make the button background transparent use the following attribute as this will not affect the material design animations:


style="?attr/buttonBarButtonStyle"



There are many ways to style your button. Check out this tutorial.



Second, to make the text bold on pressed, use this java code:


btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {
// When the user clicks the Button
case MotionEvent.ACTION_DOWN:
btn.setTypeface(Typeface.DEFAULT_BOLD);
break;

// When the user releases the Button
case MotionEvent.ACTION_UP:
btn.setTypeface(Typeface.DEFAULT);
break;
}
return false;
}
});



I achieved this with in XML with android:background="@android:color/transparent"



You can achieve that by setting the colors alpha channel.



The color scheme is like this #AARRGGBB there A stands for alpha channel(transparency), R stands for red, G for green and B for blue.



Step 1:
Create a new resource file in drawable and copy paste


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<stroke android:color="#fff" android:width="2dp"/>
<corners android:radius="25dp"/>
<padding android:right="15dp" android:top="15dp" android:bottom="15dp" android:left="15dp"/>
</shape>



save it as ButtonUI(let's say)



Step 2: Apply the UI to the button xml


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="join the crew"
android:background="@drawable/ButtonUI"
android:textColor="#fff"/>



You apply the background color as transparent(light gray) when you click the button.


transparent(light gray)


ButtonName.setOnClickListener()



In the above method you set the background color of the button.





Worrying that this was upvoted. Don't do this, please. The future maintainer will curse your name. Set a background with a selector, and have one color/drawable for pressed and one for not. Don't write code to change it in the listener - that is NOT how this is done.
– themightyjon
Jul 17 '15 at 11:25



Code:


button.setVisibility(View.INVISIBLE);



Xml:


android:background="@android:color/transparent"



You can do it easily by adding below attribute in xml file. This code was tested plenty of time.


android:background="@android:color/transparent"






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C++ thread error: no type named ‘type’ MINGW

Decreasing a static image progress bar horizontally in SDL

How to input without newline? (Python)