i have a ImageView in the layout, when i click on the image i want to get that image into a variable and replace with another image in this ImageView. please help me..
The onClick Listener will give you a View, that's the ImageView that was clicked. Cast it to an ImageView and do whatever you want with it.
in this example i have take previous image in Drawable and replace i with new image. if you set any imageview to image which stay in drawable variable(d) then use :: setBackgroundDrawable(d); is useful
public void onClick(View v){
ImageView i;
i = (ImageView) findViewById(R.id.img);
Drawable d = i.getBackground();
i.setBackgroundResource(R.id.secondImage);
}
Related
Shopping Flyer A viewpager contain a firebase image.This image contain many small different images.When I touch a small image I have to display to another activity.how to proceed to achieve this ?
Use a GridLayout, with ImageView children. Use the onClickListener of the ImageView when they were added to the GridLayout to determine which image was clicked.
You don't need to know the coordinates of the image. Because the ImageView has its own space.
GridLayout gridLayout;
ArrayList<MyImage> myImages; // MyImage model should contain the image and information about the product
for (MyImage myImage : myImages) {
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(myImage.getImageBitmap());
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// start your details activity here
}
});
gridLayout.addView(imageView);
}
I have two images with transparent background.
I added both to the layout view.
I need to assign an onClickListener to each image.
The problem is that only the topmost image is firing the click event (is like if the top most image cover the rest and don't care about its transparent background). Both images are .png with transparent background
Here is the code:
ImageView img1, img2;
RelativeLayout l = (RelativeLayout)findViewById(R.id.layout1);
bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.image2);
img1 = new ImageView(this);
img1.setImageBitmap(bm1);
l.addView(img1);
img2 = new ImageView(this);
img2.setImageBitmap(bm2);
l.addView(img1);
img1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
//code to process when img1 is clicked
}
});
img2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
//code to process when img2 is clicked
}
});
In this example, only img2 is firing the event.
I need that both img2 and img1 can fire their events.
Thanks in advance for your help.
If the transparent background of one image covers the second, then the first will respond as being clicked, because the transparent background is counted as part of the image.
Perhaps the simplest answer would be a little bit of photo-editing to remove some of the transparent background. Crop the images a bit.
I am resizing an ImageView and auto fitting the image to it, I need this for zoom in and out for image, this is the way i wish to implement this because the image is already 2048x2048 and I cant re-size it. Bu the code only works once for some reason, second click does not do anything. If you have other ideas or suggestions I would love to hear them. Thank you.
ImageView map = (ImageView) findViewById(R.id.imageViewMap);
Bitmap mapIm = BitmapFactory.decodeResource(getResources(),R.drawable.myMap);
map.setImageBitmap(mapIm); //first view
zoomIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
map.getLayoutParams().height = counter*5000 ;
map.getLayoutParams().width = counter*5000;
BitmapDrawable bmd = new BitmapDrawable(mapIm);
map.setImageDrawable(bmd);
bmd = null;
counter++;
}
});
For zooming your image view
imageview.setScaleType(ScaleType.FIT_XY);
or you can try the following link hope it may help you
zoomableimageview
try this hope it may help you
example zoom
I need to show a image, when i press a button on my application.
I'm trying with:
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageDrawable(R.drawable.loading);
but it shows an error.
Anyone can help me? Thanks.
import package_name.R.drawable;
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageResource(drawable.loading);
OR
you can set the image from the start, set it to invisible and when you want to show it just change the visibility property.
for future visitors:
sometime setImageDrawable(Drawable) method show an error like:
The method setImageDrawable(Drawable) in the type ImageView is not applicable for the arguments (int)
can try following codes:
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(R.drawable.your_image);
imageView.setImageDrawable(image);
--------- OR can try this ------------------
imageView.setImageDrawable(ContextCompat.getDrawable(YourActivity.this, R.drawable.your_image));
hope it work for you.
I have a imageButton in android which user clicks. Then after his click I want t0 show a tick or a cross image on the toip of it.
How is it possible?
<ImageButton android:layout_width="wrap_content"
android:text="Button" android:id="#+id/button1"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal"
android:background="#drawable/rhino" android:layout_marginRight="20dp"></ImageButton>
put your ImageButton and the tick image in a FrameLayout and make the visbility of the Tick image "Invisible" . So when you click on the ImageButton then change the state of Tick Image to Visible.
Get a reference to your ImageButton and then use one of its setImage methods, such as setImageResource.
You can achieve the same using ImageView. Use ImageView
testimage = (ImageView) findViewById(R.id.imageview);
testimage.setOnClickListener(listener);
write the logic to set both type of image to imageview in onclick event
public OnClickListener listener=new OnClickListener(){
#Override
public void onClick(View arg0) {
System.out.println("..set image button..");
Drawable[] layers = new Drawable[2];
layers[0] = getResources().getDrawable(R.drawable.btn_call);
layers[1] = getResources().getDrawable(R.drawable.blue_unfocus);
System.out.println(layers[1]+"...Drawable..."+layers[0]);
LayerDrawable layerDrawable = new LayerDrawable(layers);
testimage.setImageDrawable(layerDrawable);
}
};
Thanks
Deepak