Android Transparent TextView?

128,840

Solution 1

Please try this piece of code..

<TextView 
   android:id="@+id/txtview1"
   android:layout_width="wrap_content" 
   android:background="@drawable/bg_task" 
   android:layout_height="wrap_content" 
   android:textSize="14sp" 
   android:singleLine="true"
   android:textColor="#FFFFFF" />

Used background image as transparent so may be solved that.

OR

android:background="#07000000"

OR

Please try below ...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center_horizontal" android:orientation="vertical"
    android:background="@drawable/main_bg">
    <ImageView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/header"
        android:src="@drawable/btn_complete" />
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView android:id="@+id/list" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:layout_weight="1"
            android:paddingRight="5dp" android:scrollbarStyle="outsideOverlay"
            android:cacheColorHint="#00000000" />
        <TextView android:id="@+id/footer" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:textSize="25sp"
            android:singleLine="true" android:background="#07000000"
            android:textColor="#FFFFFF" android:text="rrrrr"
            android:layout_centerInParent="true"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>
</LinearLayout>

Solution 2

See the Android Color resource documentation for reference.

Basically you have the option to set the transparency (opacity) and the color either directly in the layout or using resources references.

The hex value that you set it to is composed of 3 to 4 parts:

  • Alpha (opacity), i'll refer to that as aa

  • Red, i'll refer to it as rr

  • Green, i'll refer to it as gg

  • Blue, i'll refer to it as bb

Without an alpha (transparency) value:

android:background="#rrggbb"

or as resource:

<color name="my_color">#rrggbb</color>

With an alpha (transparency) value:

android:background="#aarrggbb"

or as resource:

<color name="my_color">#aarrggbb</color>

The alpha value for full transparency is 00 and the alpha value for no transparency is FF.

See full range of hex values below:

100% — FF
 95% — F2
 90% — E6
 85% — D9
 80% — CC
 75% — BF
 70% — B3
 65% — A6
 60% — 99
 55% — 8C
 50% — 80
 45% — 73
 40% — 66
 35% — 59
 30% — 4D
 25% — 40
 20% — 33
 15% — 26
 10% — 1A
  5% — 0D
  0% — 00

You can experiment with values in between those.

Solution 3

Below code for black:-

<color name="black">#000000</color>

Now if i want to use opacity than you can use below code :-

 <color name="black">#99000000</color> 

and below for opacity code:-

Hex Opacity Values

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00

refer Understanding colors on Android (six characters)

Solution 4

Use this:

android:background="@android:color/transparent"

Solution 5

<TextView android:alpha="0.3" ..., for example.

Share:
128,840
iTurki
Author by

iTurki

Updated on February 03, 2022

Comments

  • iTurki
    iTurki over 2 years

    Simply, how to make a TextView transparent? (Not full transparency)

    I searched the docs and the StackNetwork and couldn't find it? I guess there is something like this.

    Thanks.

    UPDATE

    This is the XML code:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:background="@drawable/background">
    
        <ImageView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/header"
        android:src="@drawable/logo1"
        />
        <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:paddingRight="5dp"
        android:scrollbarStyle="outsideOverlay"
        android:cacheColorHint="#00000000" />
    
    
        <TextView
        android:id="@+id/footer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:singleLine="true"
        android:background="#07000000"
        android:textColor="#FFFFFF"
        android:text="rrrrr" />
    
     </LinearLayout>
    

    I want the footer TextView to be transparent so that the ListView items can be seen while scrolling

  • iTurki
    iTurki almost 13 years
    what do you mean by: Setting alpha of colour 00 ?
  • iTurki
    iTurki almost 13 years
    Doesn't work! I updated the question with my XML code including yours also. Check it plz
  • iTurki
    iTurki almost 13 years
    Works great. It makes the TextView transparent. But still I can't make ListView's items appear behind it ! Any help ?
  • iTurki
    iTurki almost 13 years
    The #07000000 works now. But still I can't make ListView's items appear behind it ! Any Idea ?
  • Chirag
    Chirag almost 13 years
    can you post your screen shot please ?
  • iTurki
    iTurki almost 13 years
    Really great. I understand it now. However, I can't make ListView's items appear behind the TextView ! The whole purpose of making the TextView transparent is to make list items appear behind it. Please take a look at the XML code and tell me what's wrong ?
  • iTurki
    iTurki almost 13 years
    REALLY GREAT. You solve it dude :D . You just need to add this to the TextView tag: android:layout_alignParentBottom="true" to align it to the buttom. Thanks @Nik
  • Krishna Prasad
    Krishna Prasad about 11 years
    @Videre Thank you very much for provide a great answer
  • Simon
    Simon about 9 years
    Thanks - This is a great guide!
  • Milos Miskone Sretin
    Milos Miskone Sretin almost 9 years
    Simple and still does the job. Nice solution.
  • Ola Ström
    Ola Ström over 2 years
    The alpha (or transparent) color is set by the first to hex-digits after the # in an "aarrggbb" (a=alpha, r=red, g=green b=blue) color space. So any color starting with #00 would be completely transparent.
  • Ola Ström
    Ola Ström over 2 years
    This one is the best since it's so easy to know what the code "color/transparent" does when reading it again later.