Search code examples
androidtextviewimageview

Is there any way to make background image dark in Android


I want to darken the background of a Textview and when I lower the alpha of the background it kinda becomes white. Can anyone help? Here is my code

   <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:gravity="center">
            <ImageView
                android:id="@+id/bg_image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/abg32"
                android:alpha="0.6"
                android:scaleType="centerCrop"/>

            <TextView
                android:id="@+id/text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:padding="10dp"
                android:textAlignment="center"
                android:textSize="17sp"
                android:textColor="@color/colorWhite"
                tools:text="@tools:sample/lorem/random" />
        </RelativeLayout>

I have tried using the tint attribute but it doesn't work.


Solution

  • First Approach:

    I can see that you have mentioned the tint attribute didnt work, could you provide the code you wrote? It could be some error in it. Either way you could perhaps try this and see if it works.

    <ImageView
        ...
        app:tint="#6F000000"/>
    

    Second Approach:

    Add a view on-top of you ImageView and set the background as color and opacity desired

    <View
        android:id="@+id/overlay_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background=“#6F000000" />
    

    *Make the width and height as same as your ImageView's height and width