Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting transparency to buttons in android

Tags:

android

button

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>
like image 735
irfan Avatar asked Dec 23 '13 12:12

irfan


People also ask

How do you make a button transparent?

CSS Code: In this section, we will design the button using CSS property. We will use the background-color: transparent; property to set the button with transparent look. Complete Code: In this section, we will combine the above two sections to create a transparent background button.

How do I change transparency on android?

setAlpha(51); Here you can set the opacity between 0 (fully transparent) to 255 (completely opaque). The 51 is exactly the 20% you want.

Is there a color code for transparent?

You can actually apply a hex code color that is transparent. The hex code for transparent white (not that the color matters when it is fully transparent) is two zeros followed by white's hex code of FFFFFF or 00FFFFFF.


11 Answers

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)

like image 147
Chintan Khetiya Avatar answered Sep 26 '22 13:09

Chintan Khetiya


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

like image 22
Guillermo Merino Avatar answered Sep 23 '22 13:09

Guillermo Merino


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

like image 29
Gurfuffle Avatar answered Sep 22 '22 13:09

Gurfuffle


Use this code in your background color

android:background="?android:attr/selectableItemBackground"
like image 38
basti Avatar answered Sep 26 '22 13:09

basti


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.

like image 38
nikvs Avatar answered Sep 26 '22 13:09

nikvs


To set the background of button as transparent do:

    android:background="@android:color/transparent"
like image 43
jacob Avatar answered Sep 24 '22 13:09

jacob


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>
like image 31
Nambi Avatar answered Sep 26 '22 13:09

Nambi


You can try setting

android:alpha="0.7"

property on your Button in xml layout file

like image 38
gile Avatar answered Sep 24 '22 13:09

gile


Instead of this:

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

you can use this:

android:background="#00FFFFFF"

like image 43
Aditya Singh Rajput Avatar answered Sep 25 '22 13:09

Aditya Singh Rajput


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%

like image 36
brandon rains Avatar answered Sep 24 '22 13:09

brandon rains


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

like image 32
Abhishek Raj Avatar answered Sep 24 '22 13:09

Abhishek Raj