Search code examples
androidtextviewtransparencyopacity

How to make image background of textview semi transparent but not the text?


I need to set an image background of a textview dynamically and make it semi transparent, with text with full opacity.

Something like this does, but compatible with <21 API.

android:backgroundTint="#80FFFFFF"
android:backgroundTintMode="src_over"

When I use

textView.setBackgroundResource(x.getPicture());
textview.getBackground().setAlpha(80);

it works but later when I use the same resource to an imageview it remains transparent even though I use

imageView.setImageAlpha(255);

or

imageView.setAlpha((float) 1.0);

And this affects whole textview including the text itself.

textView.setAlpha((float) 0.5);

I tried everything I could find but didn't find a solution. What am I doing wrong?

Whole layout xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg1">

<include
    android:id="@+id/top_toolbar_details"
    layout="@layout/toolbar"
    android:layout_width="0dp"
    android:layout_height="68dp"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="0dp" />

<LinearLayout
    android:id="@+id/ll_graphics"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/top_toolbar_details">

    <!--This is the imageview-->
    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:keepScreenOn="true"
        android:longClickable="true"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_toolbar_details" />

    <VideoView
        android:id="@+id/vv_video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@+id/iv_image"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_toolbar_details"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <ScrollView
        android:id="@+id/sv_scroller"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="vertical">

        <!--This is the textview-->
        <TextView
            android:id="@+id/tv_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="20"
            android:paddingBottom="20dp"
            android:paddingEnd="20dp"
            android:paddingStart="20dp"
            android:paddingTop="10dp"
            android:scrollbars="vertical"
            android:textColor="@color/black"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="@+id/iv_image"
            app:layout_constraintBottom_toTopOf="@+id/linearLayout"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/top_toolbar_details" />
    </ScrollView>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="5dp"
    android:orientation="horizontal"
    android:weightSum="1"
    app:layout_constraintBottom_toTopOf="@+id/llImg"
    app:layout_constraintHorizontal_bias="0.505"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent">

    <ImageButton
        android:id="@+id/b_last_viewed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/circle_shape_drawable"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_previous"
        app:layout_constraintBottom_toTopOf="@+id/llImg"
        app:layout_constraintHorizontal_bias="0.773"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/b_to_all"
        app:layout_constraintTop_toBottomOf="@+id/vv_video"
        app:layout_constraintVertical_bias="0.97" />

    <ImageButton
        android:id="@+id/b_to_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="20dp"
        android:adjustViewBounds="true"
        android:background="@drawable/circle_shape_drawable"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_grid"
        app:layout_constraintBottom_toTopOf="@+id/llImg"
        app:layout_constraintTop_toBottomOf="@+id/vv_video"
        app:layout_constraintVertical_bias="0.95"
        tools:layout_editor_absoluteX="176dp" />

    <ImageButton
        android:id="@+id/b_to_favourites"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@drawable/circle_shape_drawable"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_favourite"
        app:layout_constraintBottom_toTopOf="@+id/llImg"
        app:layout_constraintHorizontal_bias="0.164"
        app:layout_constraintLeft_toRightOf="@+id/b_to_all"
        app:layout_constraintRight_toRightOf="parent" />
</LinearLayout>

<LinearLayout
    android:id="@+id/llImg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    tools:layout_editor_absoluteX="0dp">

    <android.support.v17.leanback.widget.HorizontalGridView
        android:id="@+id/gv_small_preview"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/b_last_viewed"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_editor_absoluteX="0dp" />

</LinearLayout>
</android.support.constraint.ConstraintLayout>

Solution

  • just realign your layout

    //this is your root
    <RelativeLayout>
         //this is your background
         //add your semi transparent for below imageView
         <ImageView android:height="match_parent" android:witdh="match_parent"/>
    
         //this is your text
         <TextView/ >
    
    </RelativeLayout>
    

    then programmatically

    imageView.setImageAlpha(255); //it will not affect the textview
    

    or just add android:alpha in xml at imageView