Android Forums

Go Back   These FORUMS ARE DISABLED!! OUR NEW FORUMS ARE AT http://androidforum.com/ > Android Coders > Android Development, Answers, Tutorials, and Code Snippets
Connect with Facebook

Click Here To Register!
 
 
LinkBack Thread Tools Display Modes
Old 07-26-2010, 09:16 AM   #1
New Member
 
Join Date: Jul 2010
Posts: 6
Friends: 0
View MJLcodee's Profile   View MJLcodee's Photo Album   View MJLcodee's Blog   View Social Groups
So we know how to make a certain image. You just write into the xml:
android:src="@drawable/myimage"

But how do you change this source image while the program is running, i.e. inside the Java code?

I have essentially a listactivity which makes the same image appear multiple times. Now I'd like to make it so that, for each item it lists:
if(condition1)
{make image1 appear there}
else if(condition2)
{make image2 appear there}


I tried stuff like
findViewById(R.id.ImageView01).setBackgroundDrawab le(drawable/image2);
(or do I have something mixed up, what's the backgrounddrawable? Cause I didn't see any other drawable you could "set")

and I tried findViewById(R.id.ImageView01)).setVisibility(0); , figuring I could cheat and just put the two images side by side, and make the one invisible that doesn't apply.
But both these attempts just make the program crash.

Any help would be appreciated, thanks.
MJLcodee is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-27-2010, 12:04 AM   #2
Android developer
 
Join Date: Jul 2010
Posts: 11
Friends: 0
View chandan's Profile   View chandan's Photo Album   View chandan's Blog   View Social Groups
Try the following:

findViewById(R.id.ImageView01).fly.setBackgroundDr awable(getResources().getDrawable(R.drawable.your_ image_name));
chandan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-27-2010, 03:05 AM   #3
New Member
 
Join Date: Jul 2010
Posts: 6
Friends: 0
View MJLcodee's Profile   View MJLcodee's Photo Album   View MJLcodee's Blog   View Social Groups
It says "fly cannot be resolved or is not a field". Do I need to import something?
MJLcodee is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-27-2010, 11:43 PM   #4
Android developer
 
Join Date: Jul 2010
Posts: 11
Friends: 0
View chandan's Profile   View chandan's Photo Album   View chandan's Blog   View Social Groups
Hi,

I'm sorry. I just copied the code from my editor..remove "fly" i.e as below:

findViewById(R.id.ImageView01).setBackgroundDr awable(getResources().getDrawable(R.drawable.your_ image_name));


I'm sure it will work:-)
chandan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-27-2010, 11:52 PM   #5
Android developer
 
Join Date: Jul 2010
Posts: 11
Friends: 0
View chandan's Profile   View chandan's Photo Album   View chandan's Blog   View Social Groups
To make you clear: "fly" is a variable of type "ImageView" referring to the image view in xml file.

i.e suppose you have set id of image view in xml as : android:id="@+id/ImageView01"

and then in program you can have variable referring to it:

ImageView fly=(ImageView )findViewById(R.id.ImageView01);

fly.setBackgroundDrawable(getResources().getDrawab le(R.drawable.your_image_name));
/*you can use fly instead of using "findViewById(R.id.ImageView01)" every time.
* Also declare it as class member variable in order to access in all the methods in that *class.
*/
chandan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-28-2010, 05:39 AM   #6
New Member
 
Join Date: Jul 2010
Posts: 6
Friends: 0
View MJLcodee's Profile   View MJLcodee's Photo Album   View MJLcodee's Blog   View Social Groups
Thanks, we're getting closer to the problem already. It compiles now. The only problem is, it still crashes. I dissected and debugged the code to find out where the problem is, maybe you can help me? Here's the result:

int myid = R.id.ImageView01; //= 2131099649
View myview = findViewById(myid); //= null (so here's the problem. But why would this be null?)
ImageView fly=(ImageView)myview; //=null (makes sense, since the above was null)
Drawable drawi = getResources().getDrawable(R.drawable.mypic); //= BitmapDrawable (id=830060997664)
fly.setBackgroundDrawable(drawi); //"source not found", crash.
MJLcodee is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-28-2010, 07:26 AM   #7
Android developer
 
Join Date: Jul 2010
Posts: 11
Friends: 0
View chandan's Profile   View chandan's Photo Album   View chandan's Blog   View Social Groups
That is crazy:-).

Make sure you have set id to the ImageView in xml file as below:

android:id="@+id/ImageView01"

If you have already did, give some other name and try.
chandan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-28-2010, 07:40 AM   #8
Android developer
 
Join Date: Jul 2010
Posts: 11
Friends: 0
View chandan's Profile   View chandan's Photo Album   View chandan's Blog   View Social Groups
Got the bug:

change View myview = findViewById(myid); to View myview = (View)findViewById(myid);

If it still fails, do not use View. ie:
ImageView fly=(ImageView)findViewById(myid);
chandan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-28-2010, 08:41 AM   #9
New Member
 
Join Date: Jul 2010
Posts: 6
Friends: 0
View MJLcodee's Profile   View MJLcodee's Photo Album   View MJLcodee's Blog   View Social Groups
Still didn't work. :/
I tried changing that part to View myview = (View)findViewById(myid); still returns null
Already tried ImageView fly=(ImageView)findViewById(myid); , it returns null too

And I tried changing the image id, to android:id="@+id/boing".

Here's a message I got during debugging when I "went into" the line where it assigns myview or the one that assigns fly:
"The JAR of this class file belongs to container 'Android 1.6' which does not allow modifications to source attachments on its entries."

Could that have something to do with it? That I'm using the wrong version for this command?

Last edited by MJLcodee; 07-28-2010 at 08:45 AM..
MJLcodee is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 07-29-2010, 05:02 AM   #10
Android developer
 
Join Date: Jul 2010
Posts: 11
Friends: 0
View chandan's Profile   View chandan's Photo Album   View chandan's Blog   View Social Groups
Ok,,

Make sure that in manifest file, the below line has been set properly..

<uses-sdk android:minSdkVersion="4" />

/* 3 for 1.5
* 4 for 1.6
* 5 for 2.0 and so on..
*/
chandan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
 

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard switch Lou Droid Droid X 5 08-18-2010 06:34 PM
What will happen when I switch out my 8gb for 32gb? moxieluv EVO 4G 6 08-01-2010 01:10 AM
Made The Switch.... SkittlezGTI Android Chat 1 06-04-2010 11:51 AM
Need help finding a program nanllyn Android Games And Applications 0 11-10-2009 12:22 PM
How do I switch between and kill apps in memory? dattaway Android Chat 4 07-19-2009 01:37 PM


Unlocked G1 Phones | Buy T-Mobile G1 | Google Phone

All times are GMT -6. The time now is 08:29 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.
Copyright (c) 2012 TalkAndroid.com. All rights reserved.