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>
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.
setAlpha(51); Here you can set the opacity between 0 (fully transparent) to 255 (completely opaque). The 51 is exactly the 20% you want.
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.
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)
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
Try android:background="#70FF0000"
in your button code. Works for me.
Use this code in your background color
android:background="?android:attr/selectableItemBackground"
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.
To set the background of button as transparent do:
android:background="@android:color/transparent"
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>
You can try setting
android:alpha="0.7"
property on your Button in xml layout file
Instead of this:
android:color="#66FF0000" // Partially transparent red
android:alpha="0.25" // 25% transparent
you can use this:
android:background="#00FFFFFF"
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.
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>
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 :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With