irfan
irfan

Reputation: 869

setting transparency to buttons in android

I want to make Buttons with different transparency levels in android.I have used "@android:color/transparent". But it makes the button 100% transparent. I need a 70% transparent button. Here is the XML code that I am working on:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:gravity="center" 
    android:layout_weight="1">

    <Button android:id="@+id/one" 
        android:text="@string/dtmf_1"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1" 
        android:textColor="@color/white" ></Button>
    <Button android:id="@+id/two"  
        android:text="@string/dtmf_2"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1"  
        android:textColor="@color/white" ></Button>
    <Button android:id="@+id/three" 
        android:text="@string/dtmf_3"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1"  
        android:textColor="@color/white" ></Button>

</LinearLayout>

Upvotes: 30

Views: 109932

Answers (13)

Abhishek Raj
Abhishek Raj

Reputation: 103

For creating a button transparent with dark grey as border with the text to be visible too.

  1. Create a new empty drawable file and named it: button_transparent.xml

Copy Paste the below code into it:

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

<corners android:radius="20dp" />
<gradient
    android:endColor="@android:color/transparent"
    android:startColor="@android:color/transparent" />
<stroke
    android:width="2dp"
    android:color="@color/grey" /> <!-- color name="grey">#5b5b5b</color> -->

</shape>
  1. Now into your yourActivityName.xml, paste the following code:

    <Button
     android:layout_width="@dimen/dp_120"
     android:layout_height="@dimen/dp_35"
     android:layout_margin="8dp"
     android:background="@drawable/button_transparent"
     android:text="Edit profile"
     android:textAllCaps="false"
     android:textColor="@color/grey"
     android:textStyle="bold" />
    

That's all you need to do. Your button will look like this : enter image description here

Upvotes: 1

brandon rains
brandon rains

Reputation: 11

You can set transparency with color. In Android color is broken into 4 8-bit (0-255) segments, with the first 8-bit (0-255) controlling the alpha. For example with the basic color code for Light Gray: D3D3D3. If I want light grey transparent, add 0 and to light grey and get 0D3d3d3 for 100% transparent, or 227(E3 in hex) and get E3D3D3D3 for 50% or 255(FF in Hex) and get FFD3D3D3 for 0%

Upvotes: 1

K.vijaiarivalagan
K.vijaiarivalagan

Reputation: 209

Just Use '@null' to background of button makes transparency in button

android:background="@null"

Upvotes: -1

jacob
jacob

Reputation: 51

To set the background of button as transparent do:

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

Upvotes: 5

Aditya Singh Rajput
Aditya Singh Rajput

Reputation: 71

Instead of this:

android:color="#66FF0000"    // Partially transparent red
android:alpha="0.25"         // 25% transparent 

you can use this:

android:background="#00FFFFFF"

Upvotes: 1

basti
basti

Reputation: 81

Use this code in your background color

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

Upvotes: 7

Disha
Disha

Reputation: 21

Setting the android:alpha property for button will help... Set the colour you want and then set the alpha property.

android:color="#e5e5e5"    
android:alpha="0.25"

0.25 means 25%

Upvotes: 0

Guillermo Merino
Guillermo Merino

Reputation: 3257

You can define your own "transparent" color in styles.xml and play with the alfa of the color, for example:

<color name="Transparent">#00000000</color>
<color name="Transparent80">#80000000</color>

EDIT: second one is 50% transparency

Upvotes: 9

Chintan Khetiya
Chintan Khetiya

Reputation: 16152

Using XML

If you want to set color and along with that if you want to set transparent then you have to use that color code .

android:color="#66FF0000"    // Partially transparent red
android:alpha="0.25"         // 25% transparent 

Using java

And if you want to set dynamically (java code)then try this,

myButton.getBackground().setAlpha(64);  // 25% transparent

- i.e .INT ranges from 0 (fully transparent) to 255 (fully opaque)

Upvotes: 49

Gurfuffle
Gurfuffle

Reputation: 784

Try android:background="#70FF0000" in your button code. Works for me.

Upvotes: 6

gile
gile

Reputation: 368

You can try setting

android:alpha="0.7"

property on your Button in xml layout file

Upvotes: 2

Nambi
Nambi

Reputation: 12042

try adding this to your button android:color="#55000000""

<Button android:id="@+id/three" android:text="@string/dtmf_3"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1" 
        android:color="#55000000" <!--Here--!>
        android:textColor="@color/white" ></Button>

Upvotes: 2

nikvs
nikvs

Reputation: 1090

You can set a background for the button,then achieve the transparency by adjusting the alpha attribute of the button,

android:alpha="0.7"

Makes the opacity 70 percent.

Upvotes: 5

Related Questions