Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get resource from a fragment

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.

like image 489
AKTStudios Avatar asked Jul 20 '13 15:07

AKTStudios


2 Answers

I got this working in a fragment:

int imageresource = getResources().getIdentifier("@drawable/your_image", "drawable", getActivity().getPackageName());        
    image.setImageResource(imageresource);
like image 126
Simon Avatar answered Oct 01 '22 13:10

Simon


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.

like image 39
Kevin Avatar answered Oct 01 '22 12:10

Kevin