I have set of 26 imageviews and set of 26 images( named as a... z), I am trying to set a random image to each imageview one by one by generic code.
Getting a runtime error.
public class MainActivity extends AppCompatActivity {
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialMethod();
//this method loads the random images to imageview one by one
}
public void initialMethod() {
String temp;
TextView mytextView;
int randList[] ={26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
ImageView myImageView;
int ImageViewIdList[] = {R.id.imageA1, R.id.imageA2, R.id.imageA3, R.id.imageA4, R.id.imageA5, R.id.imageA6, R.id.imageA6, R.id.imageA8, R.id.imageA9, R.id.imageA10, R.id.imageA11, R.id.imageA12, R.id.imageA13, R.id.imageA14, R.id.imageA15, R.id.imageA16, R.id.imageA17, R.id.imageA18, R.id.imageA19, R.id.imageA20, R.id.imageA21, R.id.imageA22, R.id.imageA23, R.id.imageA24, R.id.imageA25, R.id.imageA26};
for (i = 0; i < 26; i++) {
myImageView= (ImageView)findViewById(ImageViewIdList[i]);
String ImageName = String.valueOf((char) (randList[i] + 97));
int id = getResources().getIdentifier(ImageName, "id", getPackageName());
myImageView.setImageResource(id);
}
Well first of all - always post your error. You're trying to load an ID as an image resource. Instead, put your drawable IDs in randList.
Also consider creating all those ImageViews dynamically in the code, as well as other proper coding practices (e.g. no hard coded values in code and so on, though I bet this is for a school exercise)
int randList[] = { R.drawable.image1, R.drawable.image2, ... }
imageView.setImageDrawable(randList[...]);
You have mistyped one of your commas with ' between 17 and 16
Change:
int randList[] ={26,25,24,23,22,21,20,19,18,17'16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
to:
int randList[] ={26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
try this way...
int randList[] ={26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
ImageView myImageView;
int ImageViewIdList[] = {R.id.imageA1, R.id.imageA2, R.id.imageA3, R.id.imageA4, R.id.imageA5, R.id.imageA6, R.id.imageA6, R.id.imageA8, R.id.imageA9, R.id.imageA10, R.id.imageA11, R.id.imageA12, R.id.imageA13, R.id.imageA14, R.id.imageA15, R.id.imageA16, R.id.imageA17, R.id.imageA18, R.id.imageA19, R.id.imageA20, R.id.imageA21, R.id.imageA22, R.id.imageA23, R.id.imageA24, R.id.imageA25, R.id.imageA26};
for (int i = 0; i < 26; i++) {
myImageView= (ImageView)findViewById(ImageViewIdList[i]);
String ImageName = String.valueOf((char) (randList[i] + 97));
int id = getResources().getIdentifier(ImageName, "id", getPackageName());
}
As "etan" said
I have changed the code
int id = getResources().getIdentifier(randList[i],"id", getPackageName());
to
int id = getResources().getIdentifier(randList[i],"drawable", getPackageName());
Thanks a lot etan! :)
I would like to change the imageview src based on my string, I have something like this:
ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);
String correctAnswer = "poland";
String whatEver = R.drawable+correctAnswer;
imageView1.setImageResource(whatEver);
Of course it doesnt work. How can I change the image programmatically?
public static int getImageId(Context context, String imageName) {
return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName());
}
use:
imageView1.setImageResource(getImageId(this, correctAnswer);
Note: leave off the extension (eg, ".jpg").
Example: image is "abcd_36.jpg"
Context c = getApplicationContext();
int id = c.getResources().getIdentifier("drawable/"+"abcd_36", null, c.getPackageName());
((ImageView)v.findViewById(R.id.your_image_on_your_layout)).setImageResource(id);
I don't know if this is what you had in mind at all, but you could set up a HashMap of image id's (which are ints) and Strings of correct answers.
HashMap<String, Integer> images = new HashMap<String, Integer>();
images.put( "poland", Integer.valueOf( R.drawable.poland ) );
images.put( "germany", Integer.valueOf( R.drawable.germany ) );
String correctAnswer = "poland";
imageView1.setImageResource( images.get( correctAnswer ).intValue() );
I have many icon in drawable folder and I have their name as String. How can I access to drawable folder and change background imageView (or any view) use these name in dynamically. Thanks
This can be done using reflection:
String name = "your_drawable";
final Field field = R.drawable.getField(name);
int id = field.getInt(null);
Drawable drawable = getResources().getDrawable(id);
Or using Resources.getIdentifier():
String name = "your_drawable";
int id = getResources().getIdentifier(name, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
Then use this for setting the drawable in either case:
view.setBackground(drawable)
int resId = getResources().getIdentifier("your_drawable_name","drawable",YourActivity.this.getPackageName());
Drawable d = YourActivity.this.getResources().getDrawable(resId);
It can be done like this:
ImageView imageView = new ImageView(this);
imageView.setBackground(getResources().getDrawable(getResources().getIdentifier("name","id",getPackageName())));
Try this:
public Bitmap getPic (int number)
{
return
BitmapFactory.decodeResource
(
getResources(), getResourceID("myImage_" + number, "drawable", getApplicationContext())
);
}
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
if you have the filename as string you can use:
int id = getResources().getIdentifier("name_of_resource", "id", getPackageName());
with this id you can access it like always (assumed its a drawable):
Drawable drawable = getResources().getDrawable(id);
use case if not in any activity, using #FD_ examples
Note:
if you are not in any activity you have to send context param in order to use "getResources()" or "getPackageName()", and "getDrawable(id)" is deprecated, use getDrawer(int id, Theme theme) instead. (Theme can be null):
String name = "your_drawable";
int id = context.getResources().getIdentifier(name, "drawable",
context.getPackageName());
Drawable drawable = context.getResources().getDrawable(id, null);
I get the id of resource like so:
int test = (context.getResourceId("raw.testc3"));
I want to get it's id and put it into a string. How can I do this? .toString does not work.
String testString = Integer.toString(test);
Or you can use Use
String.valueOf()
eg.
int test=5;
String testString = String.valueOf(test);
Very fast to do it if you dnt remember or dnt have time to type long text :
int a= 100;
String s = ""+a;
What about:
int a = 100;
String s = String.format("%d", a);