0

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?

Eren
  • 75
  • 8

1 Answers1

0

Use this

getResources().getResourceEntryName(int resid);

or

getResources().getResourceName(int resid);

Your code should be

try {
    int resID = fields[rndInt].getInt(drawableClass);
    img.setImageResource(resID);
    String lastImageName = getResources().getResourceEntryName(resID);
    //String lastImageName = getResources().getResourceName(resID); // You can use this too
    TextView logoismi = (TextView)findViewById(R.id.logoismi);
    logoismi.setText(lastImageName);
} catch (Exception e) {
    e.printStackTrace();
}

Update

Create an array of image id's like

XML file saved at res/values/arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="my_array">
        <item>@drawable/rose</item>
        <item>@drawable/book</item>
        <item>@drawable/pen</item>
    </array>
</resources>

Read it like this

TypedArray ar = context.getResources().obtainTypedArray(R.array.my_array);
int len = ar.length();
int[] resIds = new int[len];
for (int i = 0; i < len; i++)
    resIds[i] = ar.getResourceId(i, 0);
ar.recycle();

Use it in your code like this

Random rand = new Random();
int rndInt = rand.nextInt(resIds.length) + 1;
try {
    int resID = resIds[rndInt];
    img.setImageResource(resID);
    String lastImageName = getResources().getResourceEntryName(resID);
    //String lastImageName = getResources().getResourceName(resID); // You can use this too
    TextView logoismi = (TextView)findViewById(R.id.logoismi);
    logoismi.setText(lastImageName);
} catch (Exception e) {
    e.printStackTrace();
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • But images don't change? Maybe error welded from import? http://i.hizliresim.com/gZ1ajN.png – Eren Feb 05 '16 at 21:38
  • Yes I handle it but it's taking all drawable data. :D Can I create a drawable folder and use with it? – Eren Feb 05 '16 at 21:43
  • @Eren: I suppose that is not possible. See this http://stackoverflow.com/questions/1077357/can-the-android-drawable-directory-contain-subdirectories I suppose your first approach is better in this case. – Rohit5k2 Feb 05 '16 at 21:46
  • But then I can't show images names in textView there is image1, image2, image3 Nothing suggests.. Does that have something on your mind?? – Eren Feb 05 '16 at 21:49
  • I can think of one thing. Create an array of image id's and get it using random index. Rest remains same. – Rohit5k2 Feb 05 '16 at 21:53
  • @Eren: If the code is in `Activity` then remove `context.` and if the code is in any `Fragment` then replace it with `getActivity()` – Rohit5k2 Feb 05 '16 at 22:27
  • @Eren: Yeah it. Please don't forget to accept the answer if it solved your porblem. :D – Rohit5k2 Feb 05 '16 at 22:42