I'm trying to change the source of an ImageButton in android within a fragment.
I want to use the method Image.setImageResource() , however i can't use getResources() within the fragment. is there a way round this? getActivity().getResources() does not return any results unfortuantely.
I've tried writing a string such as "R.drawable." + {different image names} but i cannot convert that string to an int.
How else could this be done?
I just want to change some imageButtons with source files dependent on different things.
I got this working in a fragment:
int imageresource = getResources().getIdentifier("@drawable/your_image", "drawable", getActivity().getPackageName());
image.setImageResource(imageresource);
R.drawable.imagename
is an int. And setImageResource()
takes an int:
imageButton.setImageResource(R.drawable.imagename)
Also, it's strange that getActivity().getResources()
would return nothing. If your Fragment is attached to an Activity, that should return a Resources object.
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