Get the image associated with an ImageView - android

I like to know whether there is any way by which we could get to know which image is associated with a particular imageview.
ImageView iv = new ImageView(r.drawable.pic);
If this was like this i could have know that imageview iv conatins "pic". But this is not the case in my app.
Recently i have been developing an app in which i have 26 imageview,and i use to set the images associated which each of these imageviews randomly. That is, I couldnt know predict which imageview will contain which image.
But i need to find out which image is actually associated which each imageview. I also have 26 images, one for each imageview.

How about when you set the tag to something you know:
ImageView image = (ImageView)whatever;
image.setTag("key", "Image ID");
then
String imageId = (String)imageView.getTag("key");
ImageView iv12=(ImageView)findViewById(R.id.imageView2);
ImageView iv13=(ImageView)findViewById(R.id.imageView3);
// ..............iv36.......
Random ran=new Random();
for (int i = image.length - 1; i >= 0; i--) {
int index = ran.nextInt(i + 1); // Simple swap
int a = image[index];
image[index] = image[i];
image[i] = a;
}
//..........................
final Drawable d1 = getResources().getDrawable(image[0]);
final Drawable d2 = getResources().getDrawable(image[1]);
ok
// Then I assume
iv1.setImageResource(d1); // ?
iv1.setTag("key", image[0]);

Related

How to dynamically get R value [duplicate]

This question already has answers here:
how to access the drawable resources by name in android
(7 answers)
Closed 6 years ago.
I have to dynamically get some resources from the R file.
For example, let's say I have to dynamically generate ImageView and dynamically get a drawable to put inside it.
I don't know how many ImageView I have to generate; there may be 10, 50 or 100 so I have to do everything dynamically.
My main problem is to get dynamically the drawable from the R file.
Lets say I have this drawable:
R.drawable.img1
R.drawable.img2
R.drawable.img3
R.drawable.img4
I should do something like this:
for(int i = 0; i < 10; i++){
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.img + i);
}
How can I "build" this line of code: R.drawable.img + i
How can I reach my goal?
At first make sure your image is jpg or png
You can try with this
for(int i = 0; i < 10; i++)
{
ImageView iv = new ImageView(this);
int imageResource = context.getResources().getIdentifier("#drawable/img "+i.replace(".jpg", ""), null,context.getPackageName());
iv.setImageResource(imageResource);
}
final int []imageArray=
{R.drawable.img1,R.drawable.img2,R.drawable.img3};
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run() {
//randomimg.setImageResource(imageArray[i]);
//hEAR CREATE dynamic image view and set image resource
i++;
if(i>imageArray.length-1)
{
i=0;
}
handler.postDelayed(this, 5000); //for interval...
}
};
handler.postDelayed(runnable, 5000);
put above code in your activity. and set image resource it work for me.

Declare ImageView programmatically Android

How can I declare some ImageViews programmatically in oncreate method ? I want to have something like this:
for (int i =1 ; i<=10;i++){
ImageView image+i = (ImageView)findViewById(R.id.image+i);
}
I don't know if it is possible something like this, or you have to make it imageview by imageview.
ImageView[] imageViews = new ImageView[10];
for (int i =1 ; i<= 10; i++){
int id = getResources().getIdentifier("image"+i, "id", getPackageName());
imageViews[i - 1] = (ImageView)findViewById(id);
}
getIdentifier returns the resource's id associate to the name, the first parameter, and you could use an array to store the view's reference.
you can use something like this method
LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageView image+i = new ImageView(this);
image+i.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image+i.setMaxHeight(20);
image+i.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
go through this tutorial and this too.

Set ImageView Drawable programmatically

I am trying to set the image of a ImageView programmatically from a array of drawables but keep getting a NUllPointerException....does this method look correct for setting Image resource..
//get a random drawable
int[] imageSelection= {R.drawable.buddycheck, R.drawable.logdive3, R.drawable.sea, R.drawable.weather, R.drawable.logo1};
Random whichImage = new Random();
int theImage = whichImage.nextInt(imageSelection.length);
displayImage.setBackgroundResource(theImage);
You are setting the image resource to a random number.
You need to do it like this:
int theImage = imageSelection[whichImage.nextInt(imageSelection.length)];
displayImage.setBackgroundResource(theImage);
Try this : imageView.setImageResource(R.drawable.image);

how to programmatically add an image to a activity android

Hi I am trying to programatically add an image to an activity for an android app
I have this :
for (int i = 0; i < num_devices; i++) {
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
try {
Class res = R.drawable.class;
Field field = res.getField(device_types.get(i));
int resId = field.getInt(null);
imageView = (ImageView) findViewById(resId);
}
catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
}
LinearLayout link_devices = (LinearLayout) findViewById(R.id.link_devices);
link_devices.addView(imageView);
however it doesnt let me get the location of the images ( i try and get 0 for getTop, getLeft etc..)
am i doing it wrong and was is the correct way to do it
You are not setting image on that imageview, and you have set WRAP_CONTENT as layout params, which means the size of the imageView is same as size of image you are setting on it.
Since no image is attached the imageView size is 0.
Try setting an image, using any one of the codes ;- imageView.setImageBitmap(Bitmap bm), setImageDrawable(Drawable drawable) or setImageResource(int ResID).
I don't get what you're doing the with this code & its not required:
Class res = R.drawable.class;
Field field = res.getField(device_types.get(i));
int resId = field.getInt(null);
imageView = (ImageView) findViewById(resId);

android:enlarging an image on click

I am making an android app in which i need to enlarge the image size on click.
I am getting these images downloaded from server and then adding these images programmatically in the activity and hence setting id of images programmatically. Now my problem is that when i send the imageid o another activity to enlarge it it is taking only the id of last image.
and hence gives the following exception.
Unable to start activity ComponentInfo{com.emx.OnDaMove/com.emx.OnDaMove.FullImageActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x5
i am using the following code for making images and adding them to table rows.
imageUrl = "http://ondamove.it/English/images/users/";
imageUrl = imageUrl + listObject.get(j).getImage();
image = new ImageView(this);
image.setPadding(20, 10, 0, 0);
image.setId(j+1);
imageId=image.getId();
tr.addView(image);
and the following method for clickliastener:
private OnClickListener clickImageListener = new OnClickListener() {
public void onClick(View v) {
Intent fullScreenIntent = new Intent(v.getContext(),FullImageActivity.class);
fullScreenIntent.putExtra(ProfilePageNormalUser.class.getName(),imageId) ;
ProfilePageNormalUser.this.startActivity(fullScreenIntent);
}
};
and the activity in which images are to be enlarged is:
ImageView myimage = (ImageView) findViewById(R.id.imageviewEnlarged);
Intent intent = getIntent();
int imageId = (Integer) intent.getExtras().get(ProfilePageNormalUser.class.getName());
System.out.println("********"+imageId +"**********");
InputStream is = this.getResources().openRawResource(imageId);
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
Matrix imageMatrix = new Matrix();
imageMatrix.postRotate(90);
Bitmap scaledBitmap = Bitmap.createBitmap(originalBitmap, myimage.getWidth(), myimage.getHeight(), originalBitmap.getWidth(), originalBitmap.getHeight(), imageMatrix, false);
myimage.setImageBitmap(scaledBitmap);
myimage.setScaleType(ScaleType.FIT_XY);
Ya surely it will give the last Id since the value of imageId will be update on every loop. This will store the last known value during looping.
You should use array instead of simple variable.
or you can use v.getId in onClick method. (simpler one)

Categories

Resources