8

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

user3104504
  • 81
  • 1
  • 1
  • 8

3 Answers3

22

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"
/>
Community
  • 1
  • 1
CSmith
  • 13,318
  • 3
  • 39
  • 42
1

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

yahya
  • 4,810
  • 3
  • 41
  • 58
0

in code, you can get an instance of the button and set the background drawable's alpha manually.

 Button btn = (Button) findViewById(..);
 btn.getBackground().setAlpha( a );  // where a : 0..255 : 0=transparent, 255=fully opaque
dangVarmit
  • 5,641
  • 2
  • 22
  • 24
  • I don't think you can set the alpha in the xml file. There's no drawable resource that allows it [see http://developer.android.com/guide/topics/resources/drawable-resource.html ]. You'll have to either set the alpha in code or make your own semi-transparent background drawable and use that – dangVarmit Dec 17 '13 at 20:58