Image Source Changing - android

I want to change an ImageView source from my code(not the xml). I have an activity and I want to change the image in it, according to a message I recieve from an other activity. Is there any method that can change the image source from the class code and not the xml code??
For example:
if (message.equals("hello")){
//change the ImageView to hello.png;
}

You can do this by following the below steps:
Save the images with message name(i.e. hello.png, etc).
Now in your if condition use below code to set the image. Change mipmap to drawable if you have saved the images in drawable folder
int imageId=context.getResources().getIdentifier(message,"mipmap",context.getPackageName());
imageView.setImageResource(imageId);

Related

Setting an ImageView from the drawable folder dynamicly

Hello developers out there,
I have written a class called Item. Now I want this "Item" to get an image in the constructor from the drawable folder. I already tried to somehow get the resourceId from the images in the drawable folder, but didn't know how, because there is now such function as imageView.getResourceId() which isn't the best solution anyway because I would need to temporarly add all images from drawable to an imageView and then get the Id. Is there any way to solve this problem?
Thank you for your help
Solution:
Just use this code in java to set the drawable image to ImageView dynamically:
your_image_view_object.setImageDrawable(ContextCompat.getDrawable(Your_Activity.this, R.drawable.your_drawable_image));
For Example:
imageView.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_profilepic));
That's it. Hope it helps.
Try this,
int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);
This will return the id of the drawable you want to access... then you can set the image in the imageview by doing the following
imageview.setImageResource(id);

Get the file name for the image used in an ImageButton

I'm using Robotium to test a screen containing ImageButton switches in an Android app. I have two ImageButtons that use two PNG images (on.png and off.png) to display the state of the switches. I'm trying to find a method that will return the file name of the currently displayed image so I can run a test like:
Get the image being displayed by ImageButton1
Verify that the image is "on.png"
Set ImageButton1 to editable state
Select ImageButton1
Get the image being displayed by ImageButton1
Verify that the image is "off.png"
How do I verify that the correct image file is being displayed by the ImageButton in the correct situation?
BTW, the image is being set in the XML element.
I suggest you to take a look at:
View.#getDrawableState
It should work like:
boolean on = imageButton.getDrawableState()[your.R.id.state_on] == 1;
boolean off = imageButton.getDrawableState()[your.R.id.state_off] == 1;
You can also check similar question
ImageButton1.getResource().toString();

How to change a resource icon in a imageview with another resource icon

I have been reading some post about changing an image in an ImageView.
But everyone asks for setting an ImageView with a custom image, and I want to set a system image inside it.
I made this:
ledicon.setImageDrawable(getResources().getDrawable(R.drawable.presence_busy));
But Eclipse tells me that presence_busy is not recognized.
Why?
I am using presence_online in the XML file,
android:src="#android:drawable/presence_online"
and all works perfectly.
Try this:
ledicon.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_busy));
(When accessing system resouces you have to look them um in android.R not your own.)

How to set background image for Gallery

I want to set the background of my Gallery by an image (say background.jpg ) in Drawable folder.
As the prototype of the setBackground() method is
void setBackground(Drawable background)
Set the background to a given Drawable, or remove the background.
But I don't know how to refer to my image in this method.
I have tried to refer like this
Gallery galleryModified;
galleryModified.setBackground(background);
But getting error in second line, saying unable to find resource.
Please reply if you know.
Thanks.
It seems, you are setting background from a resource from drawable folder, which have id, R.drawable.background, if it is the case, try following:
galleryModified.setBackgroundResource(R.drawable.background);
All resources in your project is parsed under R class automatically. You should always retrive them as an object, such as: R.drawable.drawable_name This is a basic way to retrieve many resource types. Just replace drawable keyword such as:
R.string.name_of_string_resource
R.id.id_control

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