How can I manage lots of android Imageviews? - android

I have 20 ImageViews in my Activity. I need one method to manage them easily.
My code is like this right now:
Imageview img1 = (Imageview) findviewbyid(R.id.imageview1);
img1.setbackgroundresource(R.drawable.image1);
Imageview img2 = (Imageview) findviewbyid(R.id.imageview2);
img2.setbackgroundresource(R.drawable.image2);
Imageview img3 = (Imageview) findviewbyid(R.id.imageview3);
img3.setbackgroundresource(R.drawable.image3);
.
.
.
but I need an easier way!

You can create an array of ids with all your resources:
int[] myIds = {R.id.imageview1, R.id.imageview2, R.id.imageview3, ...};
int[] myDrawables = {R.drawable.image1, R.drawable.image2, R.drawable.image3, ...};
Then iterate through it using a for loop, while, a map function (I know this is Java 6...).
for (int i = 0; i< myIds.length; i++) {
Imageview img = (Imageview) findviewbyid(myIds[i]);
img.setbackgroundresource(myDrawables[i]);
}
Although it would be better to use a GridView / ListView with an Adapter for that

It seems all your resources are not dynamically defined. Why don't you just set them in your xml:
<ImageView
android:id="#+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image1"/>

Related

Why setVisibility() does work in for-statement?

There are a lot of questions on this topic but none whose answers solve my problem.
I have an array of ImageView and, through a for-statement, I would like to get them INVISIBLE. The code is the following.
final ImageView[] image = new ImageView[12];
image[0] = (ImageView) findViewById(R.id.imageView1);
image[1] = (ImageView) findViewById(R.id.imageView2);
image[2] = (ImageView) findViewById(R.id.imageView3);
image[3] = (ImageView) findViewById(R.id.imageView4);
for (int p = 0; p < 4; p++) {
image[p].setVisibility(View.INVISIBLE);
}
It seems that the problem is putting p as argument of image[], I think so because if I put a number instead of p it works.
try to use this it uses Varargs
public void hideViews(View... views)
{ //it will work with parent class but you can change it to ImageView
for (View view : views) {
view.setVisibility(View.INVISIBLE);
}
}
and the call:
hideViews(image1,image2,image3,image4);
good luck;

"findViewById" with for loop [duplicate]

This question already has answers here:
Accessing contents of R.string using a variable to represent the resource name
(4 answers)
Android: Using findViewById() with a string / in a loop
(8 answers)
Closed 7 years ago.
it's my code:
ImageView img1 = (ImageView) findViewById(R.id.img1);
ImageView img2 = (ImageView) findViewById(R.id.img2);
ImageView img3 = (ImageView) findViewById(R.id.img3);
ImageView img4 = (ImageView) findViewById(R.id.img4);
ImageView img5 = (ImageView) findViewById(R.id.img5);
ImageView img6 = (ImageView) findViewById(R.id.img6);
ImageView img7 = (ImageView) findViewById(R.id.img7);
ImageView img8 = (ImageView) findViewById(R.id.img8);
ImageView img9 = (ImageView) findViewById(R.id.img9);
I d like to create something like this:
for (int i=1; i<=9; i++) {
ImageView img+i = (ImageView) findViewById(R.id.img+i);}
How can I do that?
Do It like this. You can get drawable names from array stored in xml or code.
for (int i=1; i<=9; i++) {
int id = getResources().getIdentifier("drawableName", "drawable", context.getPackageName());
ImageView img = (ImageView) findViewById(id);
}
Here "drawableName" is the name of your image and leave "drawable" as is.
You cannot do this in the loop.
Basically R.id.img is a integer and
if you do R.id.img + i, it will result in some random integer.

Change many imageView sources using for loop

I have 100 imageview in my layout :
iv1 = (ImageView) findViewById(R.id.ImageView1);
iv2 = (ImageView) findViewById(R.id.ImageView2);
iv3 = (ImageView) findViewById(R.id.ImageView3);
...
...
iv98 = (ImageView) findViewById(R.id.ImageView98);
iv99 = (ImageView) findViewById(R.id.ImageView99);
iv100 = (ImageView) findViewById(R.id.ImageView100);
Now, in my program I want change all image sources time to time, so now how i do this i want some thing like this
for (int F=1; F<101; i++) {
int resID = getResources().getIdentifier("a"+F, "drawable", getPackageName());
ivF.setImageResource(resID);
}
so, any suggestion ?
thanks.
Why dont you create it dynamically , set ids to them and use these ids as per your need to get image references .
I found solution by my own, this how I should declare ImageView
ImageView iv1,iv2,iv3,iv4,iv5,iv6;
ImageView[] image = {iv1,iv2,iv3,iv4,iv5,iv6}
and then when i need to set an image :
for (int F=1; F<7; F++) {
int resID = getResources().getIdentifier("a"+F, "drawable", getPackageName());
image[F].setImageResource(resID);
}

Displaying random images

im novice to android.For displaying random images i used arraylist.
ArrayList<Integer> List = new ArrayList<Integer>();
for(int i=0;i<10;i++)
{
List.add(i);
System.out.println("Random Images"+List.add(i));
}
Collections.shuffle(List);
for(int i=0;i<10;i++){
imageArr[i] = (ImageView)findViewById(id[List.get(i)]);
System.out.println("Display Images"+List.get(i));
}
}
it is running correctly in logcat but what should do to display images on emulator screen. Pls Suggest
You will need an ImageView to display Images on the Screen.
You can display drawable, bitmaps, ...
The easiest way to do this create your main.xml like this:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/myImageView" />
Then call in your code (maybe in the onCreate):
ImageView imgView = (ImageView) findViewById(R.id.myImageView);
Then make a Drawable Array or an ArryList with bitmaps, whatever.
Get a Random value (like Math.random()) and fetch a random image from array or arraylist like
Drawable drawable = drawableArray[YOURRANDOMNUMBER];
and set the Drawable to the imageview.
imgView .setImageDrawable(drawable);
Hope this helps :)
Give this a read and see if it helps you: http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/

How to add images dynamically to an ImageView

I'm writing a puzzle game to learn developing Android Apps. But now I'm stuck on how to add images dynamically to the ImageViews in the layout file (see below). I'm trying to make a loop where I add a random image to each of the ImageViews. But I can't find any examples on how to do this. My images are named the same as the ImageViews, only in lower case letters.
Or are there some other and better ways to solve this?
My code so far:
// Puzzle of 2x2 tiles
String[] sTiles = {"A0","A1","B0","B1"};
final Random myRandom = new Random();
// Random tiles
String tmp = null;
for (int i = 0; i < sTiles.length; ++i) {
tmp = sTiles[i];
int r = myRandom.nextInt(sTiles.length);
sTiles[i] = sTiles[r];
sTiles[r] = tmp;
}
// Lopp to add images randomly to the screen (layout/main.xml)
//for(i=0; i < sTiles.length; ++i) {
ImageView image = (ImageView) findViewById(R.id.B0);
image.setImageResource(R.drawable.a0);
//}
--------------- layout/main.xml ------------
<TableRow>
<ImageView
android:id="#+id/A0"
android:layout_column="1" />
<ImageView
android:id="#+id/A1"
android:gravity="right" />
</TableRow>
<TableRow>
<ImageView
android:id="#+id/B0" />
<ImageView
android:id="#+id/B1" />
</TableRow>
Thanks, Sigurd!
Try setting up a few int arrays to store your drawable and widget IDs. It's faster than using reflection to find them by name. Something like this should do the trick:
int[] imageViews = {
R.id.A0, R.id.A1,
R.id.B0, R.id.B1,
R.id.C0, R.id.C1 //...
};
int[] images = {
R.drawable.a0, R.drawable.a1,
R.drawable.b0, R.drawable.b1,
R.drawable.c0, R.drawable.c1 //...
};
Random random = new Random(System.currentTimeMillis());
for(int v : imageViews) {
ImageView iv = (ImageView)findViewById(v);
iv.setImageResource(images[random.nextInt(images.length - 1)]);
}
You may want to add some special handling if you want to ensure that all the ImageViews get unique images, but that should work as is.
An solution is read with reflection the values from R.drawable and load it as Bitmap by getting the value of the field in R.
It's something like:
public static void example() throws IllegalArgumentException, IllegalAccessException{
Class<R.drawable> clazz = R.drawable.class;
for(Field field : clazz.getDeclaredFields()){
Bitmap bmp = BitmapFactory.decodeResource(getResources(), (Integer) field.get(null));
//bmp is your image
}
}
You'll have to make some sort of container(Linear Or Relative layout possibly) and add the images to the view. Example:
RelativeLayout cbrl = new RelativeLayout(this);
RelativeLayout.LayoutParams cbp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cbVal.setLayoutParams(cbp);
for(i=0; i < sTiles.length; ++i) {
ImageView image = (ImageView) findViewById(this);
image.setImageResource(R.drawable.xX);
cbrl.addView(iv);
}

Categories

Resources