Button background as transparent

Android

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Try new way to set background transparent

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

Solution 3 - Android

You may also use: in your xml:

android:background="@null"

or in code:

buttonVariable.setBackgroundColor(Color.TRANSPARENT);

Solution 4 - Android

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.

Solution 5 - Android

Add this in your Xml - 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"/>
	

Solution 6 - Android

I achieved this with in XML with

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

Solution 7 - Android

I used

btn.setBackgroundColor(Color.TRANSPARENT);

and

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

Solution 8 - Android

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;
    }
});

Solution 9 - Android

We can use attribute android:background in Button xml like below.

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

Or we can use style

style="?android:attr/borderlessButtonStyle" for transparent and shadow less background.

Solution 10 - Android

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.

Solution 11 - Android

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

Solution 12 - Android

Code:

button.setVisibility(View.INVISIBLE);

Xml:

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

Solution 13 - Android

I'd say extend Borderless style.

<style name="Button" parent="Widget.AppCompat.Button.Borderless">
    <item name="android:textColor">@color/blue</item>
    <item name="android:textAllCaps">true</item>
</style>

Solution 14 - Android

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

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

Solution 15 - Android

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

ButtonName.setOnClickListener()

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnjuView Question on Stackoverflow
Solution 1 - AndroidSteve PomeroyView Answer on Stackoverflow
Solution 2 - AndroidMuhammad Aamir AliView Answer on Stackoverflow
Solution 3 - AndroidHunkeoneView Answer on Stackoverflow
Solution 4 - AndroidPrashamView Answer on Stackoverflow
Solution 5 - AndroidMohanrajView Answer on Stackoverflow
Solution 6 - AndroidC. SkjerdalView Answer on Stackoverflow
Solution 7 - AndroidSindri ÞórView Answer on Stackoverflow
Solution 8 - AndroidFarazView Answer on Stackoverflow
Solution 9 - AndroidShihab UddinView Answer on Stackoverflow
Solution 10 - AndroidRaphael SView Answer on Stackoverflow
Solution 11 - AndroidViraj SinghView Answer on Stackoverflow
Solution 12 - AndroidChief MadogView Answer on Stackoverflow
Solution 13 - Androidsorry_I_wontView Answer on Stackoverflow
Solution 14 - AndroidChamath JeevanView Answer on Stackoverflow
Solution 15 - AndroidPinkiView Answer on Stackoverflow