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.
Related
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 have a problem that I don't think can be solved.
I would like to change an ImageView background that is inside a row in a ListView. The row is a custom view with two TextView and an ImageView.
I have the ListView id, the number of the row that contains the ImageView i want to edit and the ImageView id, of course.
There is any way to access the right ImageView?
Thanks for any answer.
UPDATE
Better Explanation:
I have a list of device. They are loaded from an adapter, which takes values from an array.
After the list is created, i start a new activity sending an array which contains the devices address.
The other activity receive the array, scan for devices in range (Bluetooth) and get a list of those.
Then I confront the two array and if there is a device in range, I change the image on the ListView relative to that device. The index of the array is the row number in the ListView.
I don't think I need to post any code since the array sending from an activity to the other is made via Intent and that works. Debugging I have bot the array.
I just need a way to access the image in the row of that list.
I found the solution.
I'll post here the code if someone, some day will have my problem:
// We have the listview, list of online devices and the list of our devices!
// Init ListView
ListView device_list = (ListView) findViewById(R.id.deviceListView);
// Loop for all the online devices found
for (int j = 0; j < mLeDevices.size(); j++){
// Get the online device address
BluetoothDevice device = mLeDevices.get(j);
String deviceAddress = mLeDevices.get(j).getAddress();
// Loop for all the devices in the ListView
for (int i = 0; i < device_address.size(); i++) {
// Compare devices address
if (device_address.get(i).compareTo(deviceAddress) == 0) {
int visiblePosition = device_list.getFirstVisiblePosition();
View v = device_list.getChildAt(i - visiblePosition);
ImageView state = (ImageView) v.findViewById(R.id.item_state);
state.setImageResource(R.drawable.led_on);
}
}
}
The list will have no more than 5 elements, so no problems with the list scrolling and losing the index.
I Actually would like to ask a question. Is it right to assume that the position in the ListView starts from 0? Or the first element index is 1?
If i understand your question right then you can use the following idea
If you know the position of the image view then you can try something like this in your getView method
imageView.setBackground(R.drawable.normalBackground);
if(position==yourRequiredPosition){
imageView.setBackground(R.drawable.newBackground);
}
this way it will always set normalBackground for imageView in each row, but for the yourRequiredposition row newBackground will be set. Only thing is you have to have a way to figure out "yourRequiredPosition"
I am developing an Android game application.I have implemented the all the basic functionality.Designed all the screens.Now the main problem i am struck is I have to select 25 images randomly from 40 images.And display all the 25 images in the application.Can anyone guide me,,,, Thanks in advance
Regards
Tushar Sahni
I am assuming that you have all drawables in your drawable folder for imageview.
See the below implementation I have done in my app. First take Imageview array and int array with length of number of images you have.
ImageView i[]=new ImageView[49];
int id[]={R.drawable.pic1,......,R.drawable.pic40};
Then take arraylist like below and add no 1 to 40 to it.
ArrayList<Integer> ranArr = new ArrayList<Integer>();
for(int i=0;i<(40);i++)
{
ranArr.add(i);
}
Then shuffle it so your randomness will generate.
Collections.shuffle(ranArr);
Now your random no logic is over so you can now use that to imageview like below
for(int i=0;i<25;i++)
{
i[i]=(ImageView)findViewById(id[ranArr.get[i]);
}
So it will generate random images from your 40 drawables.
use Random class to generate 25 numbers, which are less than 40. Then get the images with generated numbers.
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.