I have a number of images in my directory.
I want to show random images in ANDROID.
Please anyone provide me with an example.
Assume that your images are named img1.png, img2.png, and so on, and they are located in res/drawable folder.
Then you can use the following code to randomly set an image in an ImageView
ImageView imgView = new ImageView(this);
Random rand = new Random();
int rndInt = rand.nextInt(n) + 1; // n = the number of images, that start at idx 1
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setImageResource(id);
I don't have an example but I can provide you an idea.
Build a list of images in an array
Generate a random number between 0 to 1 less than the number of images in folder
Use the random number in step 2 as an index to the array and pick up the image for display.
You have to combine some things. First you need an ImageView in order to display an image on the Android phone.
Then I would take a look into a random number generator (e.g. http://docs.oracle.com/javase/6/docs/api/java/util/Random.html) so that you can get a random number.
By combining these to things, you can randomly select a picture from a list of available pictures and display it using the ImageView.
Related
I am reading data of employees from excel file stored in internal storage, unfortunately the app is totally offline and so have offline images , which i need to store in drawable folder.
Now the question i have is ;
There are 350 images named as employee id , for example
1.jpg of emp id = 1 , 2.jpg of emp id = 2.
I am using RecyclerView , in that onBindViewHolder method i have to set images according to the employee id.
Please guide me to show the image into imageView.
Please comment for more input from my side.
Plz dont mark it negative if you can't answer.
You could try something like this.
Example if trying to get employee 1526 the drawable would be R.drawable.1526
String id = employee.getId(); //1526
int imageId = context.getResources()
.getIdentifier(id, "drawable", context.getPackageName());
id being a string.
then with the image view
imageView.setImageResource(imageId);
I have is how to generate random images without repeating , as an example driving in an array 15 images and 7 images to send imageview but that are not repeated .
int[] img={ R.drawable.ima1,R.drawable.ima2,R.drawable.ima3,R.drawable.ima4,R.drawable.ima5,R.drawable.ima6,R.drawable.ima7,R.drawable.ima8,R.drawable.ima9,R.drawable.ima10,R.drawable.ima11,R.drawable.ima12,R.drawable.ima13,R.drawable.ima14,R.drawable.ima15};
int [] game= new int[7];
Random numerRan = new Random();
for (int i=0;i<game.length ;i++)
{
int num= numerRan.nextInt(17);
int x= img[num];
game[i]=img[num];
}
img1.setImageResource(juego[0]);
img2.setImageResource(juego[1]);
img3.setImageResource(juego[2]);
img4.setImageResource(juego[3]);
img5.setImageResource(juego[4]);
img6.setImageResource(juego[5]);
img7.setImageResource(juego[6]);
img8.setImageResource(juego[7]);
When I press the button control images to the score rando the position that was generated . The problem I have is that images are repeated.
I am using 2 arrangements the first is to save all the images and the second keep the random images that will assign imageview .
Instead of using an int[] array, use a List<Integer> instead, such as an ArrayList<Integer>.
That way, you'll be able to use Collections#shuffle(), which will have the desired effect. It'll randomly permute your list, in a non-repeating way. All you'll have to do later is to iterate on that List.
For more info, please refer to the Collections documentation.
You can also convert that array to a List, like the following:
List<Integer> myList = Arrays.asList(img);
For more info about that, take a look at this question.
Hello guys I'm new to android development and java programming. I hope someone can help me. I'm developing an app which is an image guessing game. I have 100 images stored in my drawable.
I currently have this code that will randomly generate an image and it will be displayed in the images view:
ImageView random_image = (ImageView) findViewById (R.id.random_level);
final int[] images = { R.drawable.img1, R.drawable.img2...R.drawable.img100 };
Random generator = new Random();
random_image.setImageResource(images[generator.nextInt(images.length - 1)]);
My question now is how can I avoid duplication of image in every level? My game composes of 25 levels.
Maybe create an ArrayList of those Image id's and whenever a user guesses an image, just remove that Image id position from that ArrayList
Example:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(R.drawable.your_images); //keep adding that, maybe using a loop would be better to add
int position = new Random().nextInt(list.size());
imageViewObject.setImageResource(Integer.intValue(list.get(position)));
//while starting next level
list.remove(position);
That's all!
If you use Random class there is a chance for repetition.
So store them into a ArrayList .So you can use Collections.shuffle(list);
It will shuffle the items in the ArrayList. So ,no head ache regarding repetition.
I'm trying to create an app that will generate random number. I found this generate number coding and it is working perfectly. However I have problem when I want to display the generate number to the layout.
For example, the generator will generate two numbers ; numberOne=3 and numberTwo=1. Then it will display imageview number 3 and imageview number 1 on the layout page. Since I'm new to this so how can I display the imageview of the numbers based on the number being generated. Thanks :)
Generate number coding
Random randomNum=new Random();
int numberOne;
numberOne=1+randomNum.nextInt(10);
System.out.println(numberOne + " ");
This is the example of the display
You just need to load the image into already defined ImageView.
// fetch image resource id
// first parameter is the image name without the extension
int id = getResources().getIdentifier("image_name", "drawable", getPackageName());
// assign it to ImageView
imageView.setImageResource(id);
HI,
I want to display 20 random images at a time over Activity screen in android. I'm using this snippet:-
ImageView imageArr[] = new ImageView[25];
int id[]={ R.drawable.letter1,R.drawable.letter2,R.drawable.letter3,R.drawable.letter4,
R.drawable.letter5,R.drawable.letter6,R.drawable.letter7,R.drawable.letter8,
R.drawable.letter9,R.drawable.letter10,R.drawable.letter11,R.drawable.letter12,
R.drawable.letter13,R.drawable.letter14,R.drawable.letter15,R.drawable.letter16,
R.drawable.letter17,R.drawable.letter18,R.drawable.letter19,R.drawable.letter20};
ArrayList<Integer> randomArr = new ArrayList<Integer>();
for(int i=0; i<20;i++) {
randomArr.add(i);
}
Collections.shuffle(randomArr);
for(int i=0; i<20; i++){
//ImageView mImageplay = (ImageView)findViewById(R.id.image_play);
imageArr[i]=(ImageView)findViewById(id[randomArr.get(i)]);
//mImageplay.setImageResource(id[randomArr.get(i)]);
}
Here I'm having 20 custom images of size 49x49 pixels(small sizes)in drawable folder which i want to dispaly over UI at a time in random fashion with no alignment.
The problem is in imageArr[i] is showing NULL value for all the 20 images,whereas "randomArr" is having the correct shuffled data.
Also, Is there any way to display it in randomized look in Layout Area as i was not able to find out solution for this.
I am struck in this and not able to resolve it. please help me out for this.
thanks in advance.
-pk
Your array id[] contains R.drawable. values (id of Drawables in your app) instead of R.id. (id of your ImageViews in your layout).
Set the android:id attributes in your layout, and use them in your id[] array.