Get position of dynamic Image View - android

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.. :)

Related

How to get the touched point of an image view

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);
}

android: one click listener on many imageviews in layout

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.

Prevent GridView reset scroll to top when element is tapped?

I have a GridView that is populated by all apps installed on the device. The user can select certain apps here. I want the selected apps to be opaque and non-selected to be partially transparent. I did this with the following:
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout linearLayout = new LinearLayout(mContext);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams layoutParamsText = new LinearLayout.LayoutParams(150, 90);
ImageView imageView = new ImageView(mContext);
TextView appLabel = new TextView(mContext);
final OurAppInfo info = (OurAppInfo) getItem(position);
if(!installedApplications.contains(info)){
AlphaAnimation alpha = new AlphaAnimation(0.4F, 0.4F);
alpha.setDuration(0);
alpha.setFillAfter(true);
linearLayout.startAnimation(alpha);
}
String appName = info.label;
if (appName.length() > 25) {
appName = appName.substring(0, 25);
appName = appName + "...";
}
appLabel.setText(appName);
appLabel.setTextColor(Color.BLACK);
appLabel.setGravity(Gravity.CENTER_HORIZONTAL);
appLabel.setTypeface(null, Typeface.BOLD);
imageView.setImageDrawable(info.drawableAppIcon);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(110, 110));
appLabel.setTextSize(15);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (installedApplications.contains(info)){
installedApplications.remove(info);
receiveUpdate(installedApplications, false, false);
} else {
installedApplications.add(info);
Collections.sort(installedApplications);
receiveUpdate(installedApplications, false, false);
}
}
});
appLabel.setLayoutParams(layoutParamsText);
linearLayout.addView(imageView);
linearLayout.addView(appLabel);
return linearLayout;
}
This is part of the GridAdapter extends BaseAdapter. The code works as expected, when I tap on an app it is either removed from or added to the list and according to transparency is set. However, whenever I tap on an element in the GridView, the view is reset and I am brought to the top of the scrollable GridView. Obviously, this isn't a problem for a small number of apps, but if you're selecting apps near the XYZ letters, every time you select one you are brought back to ABC. How can I prevent this from happening?
It looks like you're refreshing the adapter whenever you make changes that makes the grid go back to initial position. You could try saving and restoring the position before making any changes to the adapter.
//Before refreshing the adapter you get both X and Y position
int xPos = grid.getScrollX();
int yPos = grid.getScrollY();
Then you update your adapter.
After the adapter is recreated you restore the grid position:
grid.scrollTo(xPos, yPos);
You could also use (everytime possible) the method notifyDataSetChanged() instead of creating a new adapter.
Hope it helps.
Check all child views for automatic height or width.
I guess gridview calculates size of this views whenever you change data.
This was solution in my case.
In my case changed this:
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
To this:
<ImageView
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="100dp" />

Adding a view dynamically in a ClickListener

I am trying to add a View to a RelativeLayout in my OnClickListener.
montrolButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// myParent is a relative layout
// newChild is an ImageView
myParent.addView(newChild);
requestLayout();
}
});
I have looked at the HierarchyViewer, I don' see my new child being added.
Can you please tell me if I miss anything?
I just tried the very same code and it works as it should. There could be an issue with your variable myParent which is not the element you expect it to be.
Also I did not have to call requestLayout() for the added view to appear on the screen.
Possibly try to just explicitly get another part of your view and add it there to see what is happening. Also just to try, you may do this:
RelativeLayout rv = (RelativeLayout) this.findViewById(R.id.right3);
ProgressBar iv = new ProgressBar(this);
rv.addView(iv);
to see if there is anything wrong with your image view instead of myParent
In any case it works if both elements are OK - there is nothing else to do in an activity.
Do you set the layout attributes of the new view (image view)?

How to get ViewFlipper's current child position

I have added a bunch of images to a ViewFlipper and now I am performing an onClick event in the flipper. For this I would like to know the current child position so that I can perform some operations in an array. Is there anyway to find out the position of the current child.
Use this to get the current Child position:
flipper.getDisplayedChild();
And this to set child number to view:
flipper.setDisplayedChild(8);
In addflipperimages(ViewFlipper flipper) method you are adding bunch of images to ViewFlipper for that you are creating imageview, set tag to imageview, set imageview clickable true then write onclick method to imageview. go through the fallowing code it may works for you
here ids[] is an array of image ids
private void addFlipperImages(ViewFlipper flipper) {
int imageCount = ids.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
for (int count = 0; count <imageCount; count++) {
ImageView imageView = new ImageView(this);
Bitmap imbm = BitmapFactory.decodeResource(this.getResources(),ids[count]);
imageView.setImageBitmap(imbm);
imageView.setLayoutParams(params);
imageView.setTag(count);
imageView.setClickable(true);
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int id=(Integer) v.getTag();
Toast.makeText(ImageSliderVertical.this, id+"", Toast.LENGTH_LONG).show();
}
});
flipper.addView(imageView);
flipper.setTag(count);
}
}
Make use of indexOfChild().
Check this Post
How can I programmatically display a ViewFlipper's second child?
May this works for you.
I used this flipper.indexOfChild(flipper.getCurrentView())

Categories

Resources