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);
}
Related
Outline
My application has a main Image View and below main view, it has dynamic horizontal imageview .
Code for dynamic image view
ImageView imageall = new ImageView(FullImage.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(175, 175);
params.setMargins(10, 0, 0, 0);
imageall.setLayoutParams(params);
imageall.setBackgroundResource(R.drawable.imageview_border);
Picasso.with(context_tab1).load("http://example.com//upload/image/" + stringList.get(in)).into(imageall);
// Adds the view to the layout
layout.addView(imageall);
imageall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (Integer) v.getTag();
image = "http://example.com/x/upload/image/" + stringList.get(position);
Picasso.with(context_tab1).load(image).into(fullimage);
}
});
}
I populate the dynamic imageview from array list. Everything works fine till this part.
Problem
Now, On Click of the dynmic imageview must get that image view position and change as the main imageview .
This is not working for me.
For better understanding please find the image attached.
Thanks.
No need of getting position of dynamic ImageView..
While creating the dynamic ImageView you can set Ids to them and also set the onClickListener on them. Override the Onclick method, check the Id with your Ids you gave to dynamic ImageView and replace the Main view's as per the ImageView clicked..
Happy coding.. :)
I have the following scenario:
In a layout, I have imageviews aligned horizontally and vertically as shown in image.
First, which layout should be used for this purpose? RelativeLayout or FrameLayout? ListView inside the layout?
Also, instead of writing setOnClickListener for every imageview, how can I write just one click listener to get the clicked imageview?
A GridView is perfect for this. For more information on GridView, look here.
As for your onClickListener question: there is no easy way to do this, but what you can do is something like this:
Have an array containing every ImageView id like so:
public static final int[] IMAGE_VIEWS = {R.id.imageView1, R.id.imageView2, R.id.imageView3 /*etc*/}; //Make sure to list from the first imageview to the last image view in correct order
Define an onClickListener
private View.OnClickListener imageViewListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < IMAGE_VIEWS.length; i++) {
if (v.getId() == IMAGE_VIEWS[i]) {
doSomethingWithImageViewClick(i); //if ImageView 4 was clicked, variable 'i' will be 3 (because arrays start at index = 0).
}
}
}
}
Set the onClickListeners for all your imageViews:
final ImageView view1 = (ImageView) view.findViewById(R.id.imageView1);
view1.setOnClickListener(imageViewListener);
//etc
Use 'GridView' in the layout and use 'setOnItemClickListener' to handle click events,
You can find more details in below link
GridView.
GridView can achieve the effect. I think you can look at this Grid View.
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 have an ImageView, ther are 2 Images for a particular imageview, but the problem is when i click it (the clicked image is bigger then the previous one). So the layout gets shifted upwards, i dont want layout to move upwards, i just want image to extend upwards.
// addnews button the 1st screen
final ImageView addnewsback = (ImageView)findViewById(R.id.newslistback);
ImageView addnews = (ImageView)findViewById(R.id.newslistbtn);
addnews.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
addnewsback.setImageResource(R.drawable.bgbig);
vf.setDisplayedChild(0);
}
});
The correct view should be,
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);
}