Button opacity/transparency

35,415

Solution 1

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"
/>

Solution 2

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

Share:
35,415
user3104504
Author by

user3104504

Updated on December 18, 2020

Comments

  • user3104504
    user3104504 over 3 years

    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

  • dangVarmit
    dangVarmit over 10 years
    I don't think you can set the alpha in the xml file. There's no drawable resource that allows it [see developer.android.com/guide/topics/resources/… ]. You'll have to either set the alpha in code or make your own semi-transparent background drawable and use that