How to clear ImageView correctly? - android

For example, in my Activity I have such code (I skip the initialization of variables):
ImageView iview; //some ImageView
Bitmap b; //some Bitmap
iview.setImageBitmap(b);
Question is - how to clear iview resources correctly (with or without destroying view) ? Would ImageView free it's resources (used in native code) after b.recycle()?
I suppose, that ImageView doesn't just free it resources after Activity onStop (or onDestroy).

imgview.setImageResource(0);
or
imgview.setImageDrawable(null);

no you need to unbindDrawables, you can do it by setting iview.setImageDrawable(null);

You can use frequently it works:
imageView.setImageResource(0);

viewToUse.setImageResource(android.R.color.transparent);
I think using setImageResource with a color identifier will give you crashing issues on Android 2.2.1, make sure to test it.

if nothing is working for you try setting the background color of view to layout color.if my layout color is white u can do like this:
edit_countflag.setBackgroundColor(Color.parseColor("#ffffff"));
//then set the image
edit_countflag.setImageResource(R.drawable.flag_id);

Related

What is the purpose of setImageResource() in Android?

If an image is already defined in Xml and when I run the app, it pops of on the screen of the emulator without any problem, what's the purpose of setImageResource() method in Java code ?
for any dynamically programming you might need to change the content of an ImageView at runTime and for this according to your resource that might comes from web service you can do it with
setImageResource(int resId)
setImageDrawable(android.graphics.drawable.Drawable)
setImageBitmap(android.graphics.Bitmap)
We can set an image resource programmatically into ImageView by using the setImageResource() method:
Example:
ImageView img = (ImageView)findViewById(R.id.img);
// supossing to have an image called ic_play inside my drawables.
myImageView.setImageResource(R.drawable.ic_play);

ANDROID -> transparent background for imageview

In my application I have a CameraPreview. I would like to add an imageView over it so I need the imageView to have a transparent background. The src of the imageView will be a png so I could see the CameraPreview underneath.
However, it's like I've tried everything to manage the background of the ImageView Transparent:
change the alpha value (in the code AND in the xml file)
change the background to #null then to #00000000 (in the code AND in the xml file for both)
But it still not work !
Did I miss anything ?
Please ask if you need parts of my code.
P
Ok thanks for your answears but I have managed to solve my problem:
I gave a random color to the background of my layout (for example #FF34035544) and I changed the two first letters to 00 which made #0034035544.
It was finally very simple !

android change image button background

I cannot seem to change the background image of my image button. Heres the code i'm currently trying to use:
ImageButton imgButton = (ImageButton) findViewById(R.id.showSportsButton);
imgButton.setBackgroundResource(R.drawable.tab2_selected);
However this seem to be placing the new image on top of the old image leaving me with 2 images overlapping each other.
Does anyone know why this is??
For solving this question you should implement
imgButton.setImageResource(R.drawable. tab2_selected);
Use this method:
imgButon.setBackground(getActivity().getDrawable(R.drawable.your_icon));
in xml file, <Button> write: android:backgroud="#drawable/your_file"
Make sure you are on same activity. If you are changing background of different activity first create the constructor then use object to change button.
Activity obj= new activity();
obj.imgButton.setBackgroundResource(R.drawable.tab2_selected);
and also check that oncreate() method has void return type if you are using only findviewbyid.

Changing AppWidget's background dynamically with a 9 patch drawable

There is no setBackground() method in the RemoteViews class, so I've used the following workaround:
Created a FrameLayout for my app widget with an ImageView as the background view.
Changed the ImageView image using setImageViewResource() method.
Unfortunately, when it comes to 9-patch drawables this method does not work. Also when an ImageView's android:src attribute points to 9-patch - it doesn't work too. Is there any way to change the AppWidget's background image programatically using a 9-patch drawable? Thanks in advance.
EDIT
Settings the 9-patch as initial background in the XML:
<ImageView
android:id="#+id/small_widget_layout_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_lime" />
When I use android:src="#drawable/background_lime" it doesn't stretch the image properly, this code works fine. And the code to change the background from the AppWidgetProvider onUpdate method:
views.setImageViewResource(R.id.small_widget_layout_bg,
R.drawable.backgroung_lime);
This does not stretch the image as a 9-patch.
This answer was diagnosed in the above comments...
RemoteView doesn't allow access to View.setBackground(), so your workaround of using the android:src property of an ImageView is good, providing that the android:scaleType property is set to fitXY.
ImageView won't stretch it's foreground image unless you tell it to.
Please ignore if u find this trivial or irrelevant, but canT you try (assuming you are dealing with widgets):
Declaring different layouts (xml)for your widget.
Change the remoteView's source (layout.id) instead of trying to make alterations to the selected layout.
AFAIK, this is the most common approach to solving such problems. This is not perfect for two simple things I could note myself:
What do you do if you have n different "states" / "views" in your widget?
But as long as your 9-patch files are also static resources, n is painful but still theoretically manageable.
It s tedious to keep track of the changes in these parallel files.
I'd also love to find an easy way for this one...
This approach may not be an option for you also because it is basically the hard way. But it s an option nonetheless.
Suggestion #2
Have you tried using the method?
public void setInt (int viewId, String methodName, int value)
remoteView.setInt(R.id.viewid, "setBackgroundResource", R.drawable.backgroung_lime);
From another question: Change remoteView ImageView background

How to change background image of a ImageButton in android

I am facing a situation in which-
1.First I have to set the background image of a ImageButton to a default image.
2.Now at runtime based on some condition I have to change the background image of that ImageButton to some other image.
I have tried this but it doesn't work and the background image remains to default image and
doesn't change.
btn.invalidateDrawable((Drawable)getResources().getDrawable(R.drawable.info));
btn.setBackgroundDrawable((Drawable)getResources().getDrawable(R.drawable.infonewred));
How can I achieve this goal.
Have you tried btn.setBackgroundResource(R.drawable.infonewred).
I suggest you set the "default" Background directly in your XML-Layout-File via android:background.
If you have to change the Background in Runtime just use btn.setBackgroundResource(R.drawable.imagename);
since you haven't provided xml, i am guessing you are providing src to imageButton.. so to change the source of the image , what you are doing is changing the image background which will not be visible since it is the source which will be visible to you.. so to change source..
do this.
btn.setImageDrawable()
rather than..
btn.setBackgroundDrawable()
I know that's old but i got the solution as i'm also was searching for that, the correct answer is to use setImageResource , so the code will be like that:
btn.invalidateDrawable(getResources().getDrawable(R.drawable.info));
btn.setImageResource(R.drawable.infonewred);
Try the method setImageResource
Ex:
if(someCondition()){
btn.setImageResource(R.drawable.info);
} else {
btn.setImageResource(R.drawable.infonewred);
}
Just try following code to change at runtime and for default view set background property in layout xml:
if(yourCondition)
btn.setBackgroundResource(R.drawable.infonewred);
in activity file you can write
btn.setBackgroundResource(R.drawable.sliderr);
OR
in your xml layout file you can add one extra line to you button xml code
android:src="#drawable/sliderr"

Categories

Resources