Correct way of using an image resource in Xamarin.Android? - android

Currently, I'm setting backgrounds for buttons/backgrounds/textboxs/etc in the AXML using:
p1:src="#drawable/PhysicsIcon" />
For example.
And I have the image saved in my Resources/Drawable as you can see. But when I boot up my app, i'm using 280Mb+ and each new layout will add an additional 30 - 80Mb running out of memory. I know I can just allocate more memory, but that can't be the right solution. So, how can I decrease my memory footprint?
Thank you for any advice.


Image allocation OutOfMemoryError and crash in Android

I've a problem with my app relate to OutOfMemoryError and crash. I've realized two activity, first activity with a logo used to load application and a second activity with some buttons and a popup menù with some icons. Recently I've encountered problem with this popup menù that when is opened cause a crash with "OutOfMemoryError" warning. Before this crash I've used this menù without errors with only a single image repeated as test. When I've changed this image with different images it started to crash. I've added this android:largeHeap="true" to manifest too see if something changes and finally menu opens without crash but it lags when scrolling it. In application monitoring I've 112Mb used when menu is closed that goes to 204Mb when I open it. I've 115 images inside my drawable(mostly unused), about 35 active in my current Activity and additional 16 images when I open menu. Total images size is 4Mb, total used images size is 1Mb and total menu images size is 350Kb. I'm a newbie in Android but maybe I skip some conversion between png and memory in Android, but it's strange to me that 16 images(ImageButton) of 24kB with android:layout_width="85dp" android:layout_height="90dp" occupy so much memory and makes lag a new age smartphone. With this low sizes and no particular code(just xml files and some java to struct UI), how it's possible that I've this big numbers in my memory?
maybe you don't follow android standard size of icons, or the image is too large.
check this link for more info on how to use standard size in android :
Or you can use Universal Image Loader to display image see this link :

Is it bad to set images by drawable ID or through xml

I have a large amount of image views to load ( background and 80 other images). Some are buttons and some are small 10 by 10 Imageviews. Some of the images (like 20% are referring to the same image)
Right now I just load them through xml or through the code by providing resource ID. Am I doing it wrong? Should I perhaps load them all into Bitmaps first in the memory and then set them to the View? Or maybe do the "downsampling" thing.
Please let me know if what I am doing is wrong. I am worried about Out of memory issues
Its not bad to set background and some images with xml or resource id code. To do a lot of them this way is not good.
Try looking around for third party gallery libraries to help you out. I would also recommend reading about viewpagers which holds a configurable number of pages in memory and recreates others on the fly, helping manage memory usage.

Display images on all android devices

in my android application, I want to display some images of plants (i have more than 100 of them) a i also want to support different screen sizes, but my problem is i can not have images for every screen density in res/drawable folder, because size of my application horribly increase. Is there any solution how to do this? (maybe in code) Thanks a lot.
From what I understand, you want to have the text wrap around the image. You could best achieve this using this library.
As for the images, you would only need to store them at a size that looks good on the largest devices you are looking to support. For the other devices, you would just scale those down. If you're going to use the above library, you might need to set the following parameters on the ImageView in the XML file to have it scale down correctly:
Note that you will need to fiddle around with these (especially the weight) so that you get the best fit for your application.
EDIT: To conform to your requirement in the comment, the best way to go would be to use a WebView. This does mean you will need to create HTML code for your articles, which would completely change the question.
What you could try is to have a LinearLayout surrounding the image and add TextView objects to it programatically. There are good chances that this will look bad depending on the amount of text you will be adding.

Image taking too long to load in layout?

I am using ImageView to make a 1.3 MB image as background for an activity. The activity has another smaller image (the logo) and 3 buttons. There is nothing else in that activity. Now this activity takes upto 9 seconds to display. Till then the app shows a black screen. When I removed the ImageView with the 1.3 MB image, the activity loads instantly. So is this normal for ImageView to take so long to load up an image? How can I reduce this time?
You have a few options, but first you must ask why am I loading such a large bitmap?
Make the file smaller
As #laalto says, adjust the dimensions of the image and make it smaller. Probably the fastest and easiest option. As I can't see the image I can't say if this will make it look any different to the user if the resolution is smaller.
Downsample the Image
There are plenty of posts on downsampling large images to avoid outofmemory errors. Here is a good example from the AndroidQuery team, who in there own right have built a useful library that includes this sort of functionality. This is also included in the post.
You could either use their library, use their post (or similar) to write some code or you could use the excellent Picasso library from Square, although it might be overkill.
If there is a repeating background...
You have not provided the image, but if it repeats you could create a tile instead, then provide this as a resource.
So for an example you could create a new XML file called background_repeat.xml that contains
android:dither="true" />
where tile is the much much smaller image that you want to repeat.
Yes, it's normal. The image has to be decoded and scaled. It takes both processing time and memory. Memory handling triggers garbage collection every now and then, slowing the process even further. Use a profiler to find out exactly where the time is spent.
Make the image dimensions smaller to make the decoding go faster and consume less memory.
You can use android:background to set uo a background from drawable resources. And also you can do this on the root element of your xml:
<LinearLayout xmlns:android=""

Android Dev: PNG Problems?

I've tried both using XML and JAVA to create an ImageView in my Android Activity.
Both times, I am able to get all of my other PNG resources I created a day ago to display in the ImageView. The problem is, today I created a new PNG, a crop of one of the working ones, and it will not display through either method.
Is there some guide or reference that I can use to find out why my PNG will not display?
None of the images I create today are working.
ImageView speak = (ImageView) findViewById(;
I've tried using:
to no avail as well.
If you have verified that you can swap out for the images created yesterday and it works, then the problem is indeed the image. If the old ones don't work, then you changed something in your code or layouts.
Did you create them with a different program or different options?
You could try re-saving the image from a different program, or even the Yahoo smushit service. I would expect output from that service to be valid.
How did you create your crop? Are you sure it's actually a PNG? It could be a JPEG format or anything else, saved with the extension PNG. That wouldn't work. Try to do it again, and make sure you actually save it as a PNG.