Using a variable to define a resource - java

I want to display 5 random dice, of the 6 sided variety. I have code that generates 5 random integers between 1 and 6, and I have 5 ImageViews of dice. They have ids set to: d6_image_view_1, d6_image_view_2, etc. In the drawable folder, I have d1.jpg, d2.jpg, etc. So if I want to change the second picture to a 4, I would do this:
ImageView d6ImageView2 = (ImageView) findViewById(R.id.d6_image_view_2);
d6ImageView2.setImageResource(R.drawable.d4);
What if I have the numbers for the ImageView and drawable in variables? Do I have to do a long, complicated nested if setup to get the right output? Or can I somehow put those numbers right into the code? Obviously this doesn't work:
int image = 2;
int picture = 4;
ImageView d6ImageView(image) = (ImageView) findViewById(R.id.d6_image_view_(image));
d6ImageView(image).setImageResource(R.drawable.d(picture));

There are no dynamic variables in Java. Java variables have to be declared in the source code. So a solution could be to make two arrays of integers for both drawable resources and view id's
For example
int[] resourceIds = new int[] { R.drawable.d1, R.drawable.d2, R.drawable.d3, R.drawable.d4, R.drawable.d5, R.drawable.d6 }
int[] viewIds = new int[] { R.id.d6_image_view_1, R.id.d6_image_view_2, R.id.d6_image_view_3, R.id.d6_image_view_4, R.id.d6_image_view_5, R.id.d6_image_view_6 }
Then you could do
int image = 2;
int picture = 4;
ImageView imgView = (ImageView) findViewById(viewIds[image]);
imgView.setImageResource(resourceIds[picture]);

Related

Button click event to change image

Is there any method available for suppose if I click the button than the image should be change. Number of times I click the button the image should be change.
I suppose you want to change the image of an ImageView
For changing the image of an ImageView, inside the onClick() of the Button use:
myImageView.setBackgroundResource(R.drawable.my_image_name);
Here, myImageView is the object bound an ImageView, and
R.drawable.my_image_name is the location of the image file present in your Drawable folder.
For more details refer to this thread
Update
I have use a random number and switch case. When I click the button the number will be generated and pass to switch case and based on that the image should appear in the image view.
In that case, let's assume you have 3 images in your drawable folder. You can use a HashMap where the key is the index and the value is the image-path
HashMap<Integer, Integer> imageMap=new HashMap<Integer, Integer>();
imageMap.put(1,R.drawable.my_image4);
imageMap.put(2,R.drawable.my_image3);
imageMap.put(3,R.drawable.my_image2);
Now, generate Random number in range 1-3
int min = 1;
int max = 3;
int randomNumber = new Random().nextInt((max - min) + 1) + min;
Finally, set the image at the randomly generated position.
int randomImageId = imageMap.get(randomNumber);
myImageView.setBackgroundResource(randomImageId);
Hope this helps

Is there any possibility to set scale of multiple imageviews in a loop?

I want to set the ScaleX and ScaleY of multiple imageviews to the same value.
Is there a possibility to do it in a loop?
for(int i=1;i<=9;i++){
String img = "imageview" + i;
(imageview + i).setScaleX(x);
}
// x is the variable
I have not tested, but this should work
You could set a tag for each ImageView, making sure to increment each, for example:
//first ImageView
android:tag="#string/one" //one = 1
//second ImageView
android:tag="#string/two" //two = 2
Then in your for loop:
for(int i=1;i<=9;i++){
ImageView img = findViewWithTag(String.valueOf(i));
img.setScaleX(x);
}
But I would not recommend this. Instead, just set the setScaleX(x) to each individually. It's not that much code.

Reduce redundant code with ImageViews and switch cases

I have 7 ImageView's having each a switch case of 7 different cases that have the same behavior of setting images for all 7 ImageView's. Is there any way to shorten the code for this, right now I have to call each ImageView's by its name in the switch case for all the ImageView's separately and it makes the code lengthy and amateur. Like the following switch code for all imageviews being called each with their imageview names.
ImageView1 = (ImageView) v.findViewById(R.id.ImageView1);
ImageView2 = (ImageView) v.findViewById(R.id.ImageView2);
ImageView3 = (ImageView) v.findViewById(R.id.ImageView3);
ImageView4 = (ImageView) v.findViewById(R.id.ImageView4);
ImageView5 = (ImageView) v.findViewById(R.id.ImageView5);
ImageView6 = (ImageView) v.findViewById(R.id.ImageView6);
ImageView7 = (ImageView) v.findViewById(R.id.ImageView7);
switch(x){
case1:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image1));
break;
case2:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image2));
break;
case3:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image3));
break;
case4:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image4));
break;
case5:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image5));
break;
case6:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image6));
break;
case7:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image7));
break;
}
For this case, you could create an Array of ImageView and another one, with the id's of the drawables. Something like that:
int[] idOfDrawables= {R.drawable.image1, R.drawable.image2,...,R.drawable.image7};
ImageView[] bundleImageView= {ImageView1, ImageView2, ...,ImageView7};
After that you could create a method, where the logic will happen, like this:
public void setImageToImageView(ImageView[] bundleImageView, int[] idOfDrawables)
{
for (int i=0; i< bundleImageView.length; i++)
{
bundleImageView[i].setImageResource(idOfDrawables[i]);
}
}
As the others suggested, create two arrays:
int[] ids = {R.drawable.Image1, R.drawable.Image2,...,R.drawable.Image7};
ImageView[] imageViews = {ImageView1, ImageView2, ...,ImageView7};
then use this:
private void showImage(int x, ImageView iv) {
iv.setImageDrawable(getResources().getDrawable(ids[x - 1]));
}
if you want to put the image for ImageView3 call showImage(x, ImageView3)
or if you want to show the image for all:
for (int i = 0; i < imageViews.length; i++) {
showImage(x, imageViews[i])
}
Put all your images as well as your imageViews in two separate arrays in a way that corresponds to each other, then use a for loop to loop through it.
findViewById...
int[] imageIds = {R.drawable.image1, R.drawable.image2,...,R.drawable.image7};
ImageView[] imageViews = {image1, image2, ...,image7};
for (int i = 0; i < imageViews.length; i++) {
imageViews[i].setImageResource(imageIds[i]);
}
you can use butterknife open source project which helps you to decrease adding findviewbyId in your code.
Find the link for it
Butterknife open source project

Programatically setting a random image to an ImageButton

I'm trying to have the picture used by my ImageButton to be selected randomly.
I think this code should work but there seems to be a problem when passing the resource as a String.
ImageButton getClickTime = (ImageButton) findViewById(R.id.clicker);
Random generator = new Random();
int generatedRandom = generator.nextInt(10) + 1;
String randomImage = "R.drawable.bg" + (String.valueOf(generatedRandom)) ;
Drawable replaceImage = getResources().getDrawable((int) randomImage);
getClickTime.setImageDrawable((Drawable) replaceImage);
I seem to be getting in a bit of a mess with casts of ints, Strings, drawables and CharSequences.
If I manually type in a randomly selected image resource, it works. But if I pass the String to a text-box I can see that it's written exactly the same as when I type it in manually.
Can anyone see what I'm doing wrong here?
Thanks in advance
Your problem is that you are misunderstanding how Android uses resource ids. The R file holds int ids that map to resources contained in your app. You are trying to reference a drawable resource by casting its String reference into an int. That is impossible and doesn't make sense.
What I would suggest is that you create an int[] that holds the ids of all of your drawables that you want to be randomly selected.
int[] imageIds = {
R.drawable.bg1,
R.drawable.bg2,
R.drawable.bg3,
R.drawable.bg4,
R.drawable.bg5
// etc for as many images you have
};
Then, randomly select one of those drawable ids and set that your to ImageButton.
ImageButton getClickTime = (ImageButton) findViewById(R.id.clicker);
Random generator = new Random();
int randomImageId = imageIds[generator.nextInt(imageIds.length)];
getClickTime.setImageResource(randomImageId);
If you want to get the id of a resource you should use something like this:
int id = context.getResources().getIdentifier("resource_name", "drawable", context.getPackageName());
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), id);
You may want to use an array of all images and get it by random index:
int[] all = {
R.drawable.bg1,
R.drawable.bg2,
};

setImageresource in android - how to use it for many pictures in a loop

it is a rather simple question. I have 5 imageViews and 5 images in drawable folder. ImageViews are named imageView1...imageView5 and images are named image1...image5
I want how can I write the following elegantly:
imageView1.setImageResource(R.drawable.image1)
imageView2.setImageResource(R.drawable.image2)
imageView3.setImageResource(R.drawable.image3)
imageView4.setImageResource(R.drawable.image4)
imageView5.setImageresource(R.drawable.image5)
As you understand, there are not just 5 imageViews, but there are many more and I want to write it in a loop. I am having problems because R.drawable.imageX is integer not string.
//Declaration
private int[] tabDrawables = new int[] {
R.drawable.image1,R.drawable.image2,
R.drawable.image3, R.drawable.image4,
R.drawable.image5};
private ArrayList<ImageView> listImages = new ArrayList<ImageView>();
//When you find your images on the onCreate method
//add all images here
listImages.add(imageView1);
listImages.add(imageView2);
...
for (int i = 0; i < tabDrawables.length; i++) {
listImages.get(i).setImageResource(tabDrawables[i]);
}

Categories