I have an ImageView
, and its alpha is set to 0 in the XML. The XML looks like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="<height>"
android:orientation="horizontal">
<ImageView
android:id="@+id/<id>"
android:layout_width="<width>"
android:layout_height="wrap_content"
android:layout_weight="1"
android:alpha="0"
android:background="#ff00ff"
android:clickable="true"
android:focusable="true"
app:srcCompat="@drawable/<drawable>" />
<ImageView
android:id="@+id/<id>"
android:layout_width="<width>"
android:layout_height="wrap_content"
android:layout_weight="1"
android:alpha="0"
android:background="#0000ff"
android:clickable="true"
android:focusable="true"
app:srcCompat="@drawable/<drawable>" />
</LinearLayout>
I tried searching how to do this, but most questions are about how to hide the background or the entire ImageView
, and not just the background. Also, the alpha
has to be set to "0"
. Is there any way to hide only the image so that only the background
(solid color) is visible? Only the background
colors of both ImageView
s have to be displayed, so the container (LinearLayout
)'s background
cannot be used to do this for both (the background colors are different).
Changing the image's source to an image which is a solid color, the same as the background color, would work, but I need to change this color programmatically, so it might be inefficient. I've also tried different combinations of ImageView.
- setAlpha(float)
, setImageAlpha(int)
, setImageResource(int)
(to 0
) and setImageDrawable(Drawable)
(to show the image again, with the background [color]), but they produce weird results. Using setImageResource(0)
(with alpha 1f
) hides the image, showing only the solid background, but I'm not able to display the image again. Is there any easy method I'm unaware of?
I need to do this programmatically, so methods like in Java android Linearlayout set two colors wouldn't work. What is the best way to do this programmatically?