0

I am trying to make a button from an image, and the idea is that every click on the button changes the image so the shape and look of the button change too.

E.g., every click gives a point, and every shape or "image" is selected based on an algorithm I made for this purpose

I have these three variables with values generated randomly in this code below:

rand = new Random(System.currentTimeMillis());
x = rand.nextInt(3);
y = rand.nextInt(8);
z = rand.nextInt(10);

The generated numbers are assigned to the string to create the next id of the image with this code:

String myID = "R.id.myImage_" + x + y + z;

I use this code:

int resource = getResources().getIdentifier(myID, "drawable", "com.asgames.package");

However, I still get an error that the image is not found.

Community
  • 1
  • 1
Ahmad
  • 1,618
  • 5
  • 24
  • 46
  • All the images have their own IDs, which is their name: `R.drawable.image1`, `R.drawable.image2`, etc. Why do you need them to have random IDs ? – JonZarate Feb 19 '17 at 00:23
  • as I said I am new to Android,Sorry...[Updated the question] I have my random selected ID how to set the Image Button exactly as the randomly generated ID? @JonZarate – Ahmad Feb 19 '17 at 00:33
  • What are you trying to do with ImageButton's that have random ID's? – tash Feb 19 '17 at 00:41
  • Each ImageButton also has it's ID. Which is defined in the XML: `android:id="@id+/imageButton1"`, `android:id="@id+/imageButton1"`, etc. – JonZarate Feb 19 '17 at 00:42
  • Let me explain, Let's say `x=0 y=2 z=3` so that's mean the selected image is **myImage023** how I can set it as Image button? @tash @JonZarate – Ahmad Feb 19 '17 at 00:48
  • 1
    "I still get an error" -> *what* error? – Robert Columbia Sep 21 '19 at 19:06
  • the image is not found but as you can see that someone helped me and the solution worked well for me – Ahmad Sep 21 '19 at 19:07
  • Clarify via edits, not comments. And where is your [mre]? See [ask] & other [help] links. – philipxy Sep 22 '19 at 07:19

3 Answers3

4

To generate a random id for a View in android, you can use the generateViewId() which is a method in the View class bundled inside the Android library, the docs says:

Generate a value suitable for use in setId(int). This value will not collide with ID values generated at build time by aapt for R.id.

check the official documentation here.

So now you can do the following:

    int imageCoordinates = x + y + z;
    int imageId = View.generateViewId() + imageCoordinates;
Waleed Alrashed
  • 777
  • 1
  • 7
  • 20
3

You can do this by using the following code:

int resource = getResources().getIdentifier(myID, "drawable", "com.your.package");

PS: Take out the "R.id." from the name.

Aftab Safdar
  • 653
  • 5
  • 19
  • It worked weel, but It gives this error **getSlotFromBufferLocked: unknown buffer error** @Aftab Hussain – Ahmad Feb 19 '17 at 16:51
  • Apparently, this is an open issue for Marshmellow. Look at 'Itai Hanski''s answer at http://stackoverflow.com/questions/32561479/android-studio-getslotfrombufferlocked-unknown-buffer-error – Aftab Safdar Feb 19 '17 at 16:58
0
  1. Rename yuor image like image1, image2,..., image150.

  2. Generate a random number in between 1 to 150 (both inclusive).

  3. Use below code to get image resource id. (Use "mipmap" instead of "drawable" if you have placed the images in mipmap folder)

    String myID = "image"+ generatedRandomNumber;
    int resource = getResources().getIdentifier(myID, "drawable", "com.your.package");
    
  4. Use this resource id to set the image for ImageButton.

Sanjeet
  • 2,385
  • 1
  • 13
  • 22