'Black background behind image in ImageView
Seems silly but I have tried using ImageButton, Image view but the black square behind circular image is not going. I am using png image and want transparent background.
I have attached the expected output, layout view in android and real device but look very different and nothing seems to work
Edited Code XML Layout. Reset other views are fine
<androidx.constraintlayout.widget.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"
tools:context=".view.fragments.DashboardFragment">
<ImageView
android:id="@+id/iv_dashboard"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
android:src="@drawable/bg_dashboard"
app:layout_constraintBottom_toTopOf="@+id/guide_horizontal_30"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- background image resource -->
<TextView
android:id="@+id/txtFromDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/from_date"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/iv_dashboard" />
<TextView
android:id="@+id/tvFromDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/margin_10dp"
android:text="Date"
android:drawableBottom="@drawable/dashboard_bottom_blue"
app:layout_constraintEnd_toStartOf="@+id/guide_vertical_40"
app:layout_constraintStart_toStartOf="@+id/txtFromDate"
app:layout_constraintTop_toBottomOf="@+id/txtFromDate" />
<TextView
android:id="@+id/txtToDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_5dp"
android:text="@string/to_date"
app:layout_constraintBaseline_toBaselineOf="@+id/txtFromDate"
app:layout_constraintStart_toEndOf="@+id/guide_vertical_40" />
<TextView
android:id="@+id/tvToDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Date"
android:paddingTop="@dimen/margin_2dp"
android:paddingBottom="@dimen/margin_2dp"
app:layout_constraintBaseline_toBaselineOf="@+id/tvFromDate"
app:layout_constraintEnd_toStartOf="@+id/guide_vertical_80"
app:layout_constraintStart_toStartOf="@+id/txtToDate" />
<!-- problematic image view-->
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/btnGo"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/btn_search"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintBottom_toBottomOf="@+id/tvToDate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/guide_vertical_80"
app:layout_constraintTop_toTopOf="@+id/txtToDate" />
</androidx.constraintlayout.widget.ConstraintLayout>
Solution 1:[1]
use this if you don't want black background just remove background from layout.
<android.support.v7.widget.AppCompatImageView
android:id="@+id/btn_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:src="@drawable/img_key"
/>
Solution 2:[2]
I was having the same issue with my image and what I just did is to change the Background of the imageView and make it Transparent.
<ImageView
android:id="@+id/iv_dashboard"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
android:background="@android:color/transparent"
android:src="@drawable/bg_dashboard"
app:layout_constraintBottom_toTopOf="@+id/guide_horizontal_30"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Avinash |
Solution 2 | mazenqp |