I have tried the following code. It loads images quite randomly but some images appear to be the same. Can my code be modified to have all the images uniquely and randomly loaded?
int[] imageViews = {
R.id.ImageView2, R.id.ImageView2,
R.id.ImageView3, R.id.ImageView4,
R.id.ImageView5, R.id.ImageView6,
R.id.ImageView7, R.id.ImageView8,
R.id.ImageView9
};
int[] images = {
R.drawable.m1, R.drawable.m2,
R.drawable.m3, R.drawable.m4,
R.drawable.m5, R.drawable.m6,
R.drawable.m7, R.drawable.m8,
R.drawable.m9
};
Random random = new Random(System.currentTimeMillis());
for(int v : imageViews) {
ImageView iv = (ImageView)findViewById(v);
iv.setImageResource(images[random.nextInt(images.length - 1)]);
check about this solution , this was worked for me..Random images without repetition
Edit: Remove the images from the array as you're displaying them. Recalculate your random bounds to use a shorter array bound.
Related
I am making an offline daily image quote app.
How to implement random image from Notification after they clicked it? The location of the images are stored in the drawable resource folder. Any input will be much appreciated.
Drawable resource are Integers so you can create a list of ints and run a Random choose on it:
List<Integer> imgList = Arrays.asList(R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4); // add your images names
Random rand = new Random();
int pickedImg = rand.nextInt(imgList.size()); // picking random number between 0 to list size - 1
ImageView imageView = findViewById(R.id.your_image);
imageView.setImageDrawable(ContextCompat.getDrawable(requireContext(), imgList.get(pickedImg)));
I am working on a project that will display math problems to the screen such as:
10 + 5 =
and then the user will need to guess the answer. I am doing this as more of way for me to learn how android ticks.
I have images from 0 to 9 saved in the drawable folder for each of the dpi setting I need to account. I also have the operators (+,-,*,/,=) also saved.
My questions:
How easy is this to do?
How would I go about doing the above dynamically?
Thanks
--EDIT--
The images and operators are stored as .9.png files in my drawable folder. I have string-array contain my problems that I will randomly pull from and then display them to the screen.
In the past I would do the following:
public View buildProblem()
{
LinearLayout rtnView = new LinearLayout(ctx);
rtnView.setOrientation(LinearLayout.HORIZONTAL);
rtnView.setLayoutParams(new GridLayout.LayoutParams());
// Build LeftSide
Iterator<Integer> itor = buildLeftSide();
ImageView iv;
// loop through of iterator
while(itor.hasNext())
{
iv = new ImageView(ctx);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv.setImageResource(itor.next());
itor.remove();
rtnView.addView(iv);
}
// Space
rtnView.addView(space);
// Operator
rtnView.addView(plusSign);
// Build LeftSide
itor = buildRightSide();
// another loop
while(itor.hasNext())
{
iv = new ImageView(ctx);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv.setImageResource(itor.next());
itor.remove();
rtnView.addView(iv);
}
// Space
rtnView.addView(space);
// Equal Sign
rtnView.addView(equalSign);
// Space
rtnView.addView(space);
// return the view
return rtnView;
}
I was thinking of using ImageViews for the numbers to be displayed to the screen which I have used in the past but not sure if that is the best way to do it.
https://github.com/barakisbrown/MathTest -- Is one attempt at doing the above but I have now started to rewrite it from scratch so which is why I am asking for help.
I am reading data from a SOAP service and then using that data to load images from the res folder locally. I am using a loop because there will always be six images being loaded. This is my code :
final TableLayout tblLay = (TableLayout)findViewById(R.id.lottotl);
final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
for(int i=2;i<9;i++) {
final ImageView image = new ImageView(LastDraw.this);
image.setLayoutParams(trparams);
image.setMaxHeight(20);
image.setMaxWidth(20);
String imgName = "img_" + split[i].substring(split[i].indexOf("=") + 1);
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
image.setImageResource(id);
row2.setLayoutParams(params);
row2.addView(image);
}
tblLay.addView(row2);
The issue I am having is that there is a gap between the first image and the consecutive images.
It looks like this (each number representing an image):
1.........23456
I am thinking it has to do with the layout of the row in the tablelayout, I could be wrong.
Thank you
Anyone?
Figured it out ... feel kind of stupid but, I learnt something! I load a textview into the first row and then the imageviews into the second row. The textview is in the first column and its width is making the first column stretch ... THAT is why there is a gap.
I have a a set of 10 imageviews in my layout. I have given them sequential id's also as
android:id="#+id/pb1"
android:id="#+id/pb2"
Now I want to change background dynamically.
int totalPoi = listOfPOI.size();
int currentPoi = (j/totalPoi)*10;
for (i=1;i<=currentPoi;i++) {
imageview.setBackgroundResource(R.drawable.progressgreen);
}
Now inside the for loop I want to set the image view background dynamically. i,e if the currentpoi value is 3, background of 3 image views should be changed. What ever the times the for loop iterates that many image view's background should be changed. Hope the question is clear now.
Note : I have only 1 image progressgreen that need to be set to 10 image views
Finally I did this in the following way,
I placed all the id's in the array as
int[] imageViews = {R.id.pb1, R.id.pb2,R.id.pb3,R.id.pb4,R.id.pb5,R.id.pb6,R.id.pb7,R.id.pb8,R.id.pb9,R.id.pb10};
Now:
int pindex = 0;
for (pindex; pindex <currentPoi; pindex++) {
ImageView img = (ImageView) findViewById(imageViews[pindex]) ;
img.setImageResource(R.drawable.progressgreen);
}
Now, I am able to change the images dynamically.
#goto10. Thanks for your help. I will debug your point to see what went wrong in my side
Create an ImageView array:
ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.pb1);
...
views[9] = (ImageView)findViewById(R.id.pb10);
Now iterate the loop to set the background of images like this:
for (i=1;i<=currentPoi;i++)
{
views[i-1].setBackgroundResource(R.drawable.progressgreen);
}
you can do this by setting the name of drawables something like:
img_1, img_2, img_3...
for (i=1;i<=currentPoi;i++)
{
ImageView imageview=(ImageView) findViewById(getResources().getIdentifier("imgView_"+i, "id", getPackageName()));
imageview.setImageResource(getResources().getIdentifier("img_"+i, "drawable", getPackageName()));
}
Try this code.....
Create image Array..
private Integer[] mThumbIds = { R.drawable.bg_img_1, R.drawable.bg_img_2,
R.drawable.bg_img_3, R.drawable.bg_img_4, R.drawable.bg_img_5 };
And than modify your code
int totalPoi = listOfPOI.size();
int currentPoi = (j/totalPoi)*10;
for (i=1;i<=currentPoi;i++) {
imageview.setBackgroundResource(mThumbIds[i]);}
You could make an array of your ImageViews and then change them in your for loop.
ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.imageView0);
...
views[9] = (ImageView)findViewById(R.id.imageView9);
and then change your for loop to:
for (i=1;i<=currentPoi;i++) {
views[currentPoi].setBackgroundResource(R.drawable.progressgreen);
}
Arrays start at index 0, so make sure there's not an off-by-one error in here.
You'll need to give your ImageViews sequential ids, such as "#+id/pb1" and "#+id/pb2", etc.. Then you can get each of them in the loop like this:
for (i=1;i<=currentPoi;i++) {
// Find the image view based on it's name. We know it's pbx where 'x' is a number
// so we concatenate "pb" with the value of i in our loop to get the name
// of the identifier we're looking for. getResources.getIdentifier() is able to use
// this string value to find the ID of the imageView
int imageViewId = getResources().getIdentifier("pb" + i, "id", "com.your.package.name");
// Use the ID retrieved in the previous line to look up the ImageView object
ImageView imageView = (ImageView) findViewById(imageViewId);
// Set the background for the ImageView
imageView.setBackgroundResource(R.drawable.progressgreen);
}
Replace com.your.package.name with your application's package.
I am developing an app where I need to show a count. This count is going to show for example a "4", then a "4 + 1", then finally a "5". I have .png images that I want to use as background to the numbers, so that I can easily show anything up to "99" if I need to.
So basically, I would like to assign a drawable background to a number. Is a custom font the way to go? Here typeface and view group with assigned strings is mentioned. Anyone tried this?
Actually you just need an array of size 10:
Resources res = getResources();
Drawable numbers = new Drawable[10]{
new Drawable(res, R.drawable.zero),
new Drawable(res, R.drawable.one),
new Drawable(res, R.drawable.two),
...
new Drawable(res, R.drawable.nine)
};
and to get them just use:
int i = 5;
Drawable image = numbers[i];
If the number has 2 digits then you can put in 2 pictures etc.
You can also make a very big array, but that would honestly be a waste of lines.
Also the space required when you only use 10 pictures is significantly smaller
HashMap<char, Drawable> charMap = new HashMap(Character, Drawable>();
Resources res = getResources();
charMap.put('0', new Drawable(res, R.drawable.img_0));
charMap.put('1', new Drawable(res, R.drawable.img_1));
// the rest of the characters you want to use
When you want to retrieve the correct image for a character:
char c = '0';
Drawable charImage = charMap.get(c);