I have a Button and imageView, when I click button, there seems 1 of random image in imageview. Then it's names will appear in textView...
ImageView img=(ImageView)findViewById(R.id.logolar);
Random rand = new Random();
int rndInt = rand.nextInt(5) + 1;
String drawableName = "image"+ rndInt;
int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
img.setImageResource(resID);
TextView logoismi = (TextView)findViewById(R.id.logoismi);
logoismi.setText(lastImageName);
clickeddata.add(logoismi.getText().toString());
lastImageName = drawableName;
But with this code, my images names must be; image1 , image2 , image3 ... I don't wanna it. My images have different names. So I can get different images names with this code;
final Class drawableClass = R.drawable.class;
final Field[] fields = drawableClass.getFields();
final Random rand = new Random();
int rndInt = rand.nextInt(fields.length);
try {
int resID = fields[rndInt].getInt(drawableClass);
img.setImageResource(resID);
} catch (Exception e) {
e.printStackTrace();
}
But I can't get image name in my textView with this code too. How can I solve all of it?