I have image view on my project . It cant work Android Noutgat and below . But It can work For Android 8.0 and above . How Can I solve .
MyActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="MainActivity"
android:background="@color/Color_white"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:background="@drawable/backroundlegend"
/>
</RelativeLayout>
And When I delete imageView then It can works for Android Noutgat and below
Android versions has nothing to do with your case.
When you set an image to an ImageView
, you need Image loader libraries like Picasso, Glide, Fresco, Coil etc to load the image programmatically, if you don't set image like this, either the image is shown in very low quality or most of the time, image doesn't load. You can use either one but Glide is the most popular I believe.
What you've to do is give the ImageView
an ID, access it in the Java/Kotlin class file this layout is associated with and set the image to it with any of these libraries.
But first, you've to implement the library in your project. See the implementation instructions on these libraries' page I've linked above.
Then, give the ID imageView
as
<ImageView
android:id="@+id/imageView"
...
/>
Then, access it in the Java/Kotlin file as
//Kotlin
val imageView = findViewById(R.id.imageView)
//Java
ImageView imageView = findViewById(R.id.imageView);
Then, set your image as
//this is the Context here for activity, use context or requireContext() in Fragment.
Glide.with(this).load(R.drawable.backgroundlegend).into(imageView)
After this, your image will appear. Remember this, when you set svg and drawables, you don't need to load the image programmatically but when you use images like png, jpg/jpeg, you've to use image loaders. Futher, you can play with scaleType
property of ImageView
to adjust the image in the ImageView
's view bounds.