setting transparency to buttons in android

Viewed 101438

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>
13 Answers

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%

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

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

android:background="@null"
Related