Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button opacity/transparency

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:background="@drawable/background"
        android:gravity="center"
        android:color="#66FF0000"

    >


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/noua"
        android:textStyle="bold"
        android:textColor="#808080"
         />


    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/zece"
        android:textStyle="bold"
        android:textColor="#808080" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/unspe"
        android:textStyle="bold"
        android:textColor="#808080" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/doispe"
        android:textStyle="bold"
        android:textColor="#808080"
/>

</LinearLayout>

This is my main.xml, I am trying to make the buttons the most transparent, but I want still to see them and I don't know what to add and where to add, please correct my xml with the low opacity on bottons. Premeditated thank you:D

like image 836
user3104504 Avatar asked Dec 17 '13 20:12

user3104504


2 Answers

check How to Set Opacity (Alpha) for View in Android

You can set transparency on background of objects, use #XX808080, where XX is the alpha/transparency value.

android:background="#80FF0000"

this will be a half-transparent red background.

You can set transparency on the button text using the textColor property in the same manner:

android:textColor="#80FF0000"

You can also achieve through code in an animation

AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.5f);
alphaAnim.setDuration (400);
myButton.getBackground().startAnimation(alphaAnim);  // set alpha on background

or directly:

myButton.getBackground().setAlpha(0.5f);   // added in API 11, sets alpha on background

both methods set the alpha to 50%

Lastly, you can try adding android:alpha for your XML:

<Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/zece"
        android:textStyle="bold"
        android:textColor="#808080" 
android:alpha="0.5"
/>
like image 134
CSmith Avatar answered Oct 05 '22 01:10

CSmith


You probably cant change its opacity with code, because they are 9patches. You need to create your own 9patch and set it to buttons as background. If you want a solid color, of course you can also use something like:

android:background="#80363636"

Here #AARRGGBB, how low you keep first two digits you get that opacity

like image 30
yahya Avatar answered Oct 05 '22 00:10

yahya