6

I am trying to set #ffffff(white) background at 50% opacity to TextView.
I tried to use android:alpha=0.5 but it's also making text 50% transparent.

I need to set 50% opacity at white background.
I find one tutorial Here but don't understand that much.

Please provide me any reference. Thanks in Advance.

Community
  • 1
  • 1
Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160

4 Answers4

22

In your layout file just set the background of the TextView to "#8FFF"

<TextView
android:layout_width="..."
android:layout_height="..."
android:background="#8FFF"
/>

Here 8 is the alpha value, FFF are the RGB values respectivley. See here for more info on the background attribute

taylorstine
  • 905
  • 7
  • 17
  • but how you calculate this... i didn't get from your link... your answer is right... but need to understand... ow you calculate this?.. Can elaborate? – Sandip Armal Patil Dec 06 '13 at 14:02
  • 1
    These values are hexadecimal values. Each one you can think of as independent for the A, R, G, B values respectively. Hex digits are base 16, half of 16 is 8. Thus the 8. – taylorstine Dec 06 '13 at 15:13
  • then what value for black color at 50% opacity (#0000000)?. I just need to clear for my knowledge. – Sandip Armal Patil Dec 06 '13 at 15:47
  • 1
    Convert the hex values to integer values and divide by 255 to get a percent. So the largest hex value (FF=255) halved would be 80=128, so your whole answer would be #80000000. – taylorstine Dec 06 '13 at 15:56
3

In the tutorial that you mention, you will notice that the color value has two digits more that what you are specifying. You specify #FFFFFF (6 digits), whereas the tutorial specifies #CCFF0000 (8 digits). The two first digits that were add (CC) represent the alpha.

so in your case try something like #AAFFFFFF

MikeWallaceDev
  • 1,458
  • 2
  • 14
  • 28
1

50% opacity at teh white background for textview:

    <TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:alpha=".5"
     android:gravity="center"
     android:text="50% opacity"
     android:textColor="@color/white" />  

     OR

  <TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:gravity="center"
     android:text="50% opacity"
     android:textColor="#80FFFFFF" />               
Ashish Kumar
  • 1,088
  • 8
  • 17
0

Did you Try:

textView.getBackground().setAlpha(range);

//Where 0 < range <= 255, 0 being transparent and 255 being opaque