-1

I have a layout in which I am using a color and I made its opacity to 0.2. I have one textview in that layout but when I am specifying less opacity to layout then Textview text also faded. Why is that so? I only want to fade background color not textview's text.

// code of layout

 <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.2"
android:background="@drawable/background_rounded_black"
android:padding="@dimen/dp10">


<TextView
    android:id="@+id/txtNews"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:alpha="1"
    android:text="djfhwhfirhfiohrhvfuhvfjvbv  vknfjvnfjv fv vefjkhvfhvv hvufevhfuievefv ivfhiefhvfe ifvjioefv"
    android:textColor="@android:color/white"
    android:textSize="@dimen/dp15" />

<TextView
    android:id="@+id/txtTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@id/txtNews"
    android:layout_marginTop="@dimen/dp10"
    android:alpha="1"
    android:drawableLeft="@drawable/clock_news"
    android:text="12 hrs ago"
    android:textColor="@android:color/white"
    android:textSize="@dimen/dp15" />

and now how it looks http://prntscr.com/gbxi87

Deepanshi Gupta
  • 127
  • 1
  • 1
  • 7

3 Answers3

0

You set alpha tag for the parent View which is RelativeLayout. You should set alpha exactly for the View that you want to add transparency for it. Or if you want to add transparency only for background, you should use transparent color as the background of the View.

Also you said you have one TextView in your layout which is two as I see!!!

A. Badakhshan
  • 1,045
  • 7
  • 22
0

You have to change alpha on your background, not to the whole element

So remove

android:alpha="0.2"

And change just opacity of

android:background="@drawable/background_rounded_black"

Simpliest way is to create new background_rounded_black_opacity.xml in drawable folder (just copy drawable/background_rounded_black) and then change background color to what you need.

So if background_rounded_black looks like

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#000000"/>
    <stroke android:width="3dip" android:color="#B1BCBE" />
    <corners android:radius="10dip"/>
</shape>

You can add your 0.2 opacity like adding 33 to all your colors, so in your case it will be

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#33000000"/>
    <stroke android:width="3dip" android:color="#33B1BCBE" />
    <corners android:radius="10dip"/>
</shape>
Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56
0

I suggest you to set alpha color as background of textview it will work fine.

you are set opacity to that widgets so its set opacity to whole textview widgets instead you have to set only opacity to background color of textView.

you can define color with 8 digit hash code.

for example #90FFFFFF here first two digit set opacity to color.

for More info you can see this link to set opacity:

Opacity color to Hash code

Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33