I have an ImageView in a RelativeLayout:
<ImageView
android:id="@+id/image"
android:src="@drawable/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
It works on many devices (phones and tablets) and emulators, but does not work on Google Nexus 5 with Android 6. The application works perfectly but the ImageView background is not displayed.
(Image is in drawable folder and I change the image with setImageResource)
First : Check if you have added your image source to your xml layout. If you have added an image in the corresponding activity code , then the image will not appear in your xml layout.
You can make a view clickable, as a button, by adding the android:onClick attribute in the XML layout. For example, you can make an image act like a button by adding android:onClick to the ImageView .
ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android. graphics. drawable.
ImageView is used when we want to work with images or we want to display them in our application. So, this article will give you a complete idea of using an ImageView as a Button in android studio. So, without wasting further time let's go to the article and read how we can achieve this task.
I also got the same problem, in which my image show in all android version rest of Android 6.0. I was testing my application in android 5.0 and android 6.0, that time image shown properly in android 5.0 but in android 6.0 image does not load in Imageview.
I have used src and background both property but did not get the image in Imageview in android 6.0.
Then, To solve this problem I checked my logcat and I got below string in logcat.
W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (3240x5760, max=4096x4096)
I searched related this string and got the solution for this, it is Android 6.0 does not give permission to do the heavy process in UI Thread. If your image is small in size and resolution, then you can use src or background for the load it. but your image is too heavy then Android 6.0 skip this process in UI Thread.
But, You can solve this by use of below solutions.
Solution 1:
Resolve it by use of third party library like Glide for load heavy images. I have solved it with the use of this.
Solution 2:
Add a drawable-nodpi
folder in res
, put the image in it and simply use it with src in Imageview tag.
Images come in all shapes and sizes. In many cases they are larger than required for a typical application user interface (UI). For example, the system Gallery application displays photos taken using your Android devices's camera which are typically much higher resolution than the screen density of your device.
Given that you are working with limited memory, ideally you only want to load a lower resolution version in memory. The lower resolution version should match the size of the UI component that displays it. An image with a higher resolution does not provide any visible benefit, but still takes up precious memory and incurs additional performance overhead due to additional on the fly scaling.
Displays an arbitrary image, such as an icon. The ImageView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the image so that it can be used in any layout manager, and provides various display options such as scaling and tinting.
Remove android:adjustViewBounds="true"
Use Small Size (Resolution) oriented image .
You can use public void setImageResource (int resId)
Sets a drawable as the content of this ImageView.
This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageResource(R.drawable.image1);
Read Drawable-hdpi, Drawable-mdpi, Drawable-ldpi Android
To declare different layouts and bitmaps you'd like to use for the different screens, you must place these alternative resources in separate directories/folders.
This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 100x100 for mdpi, and 75x75 for ldpi devices.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With