Example usage for android.widget ImageView setOnLongClickListener

List of usage examples for android.widget ImageView setOnLongClickListener

Introduction

In this page you can find the example usage for android.widget ImageView setOnLongClickListener.

Prototype

public void setOnLongClickListener(@Nullable OnLongClickListener l) 

Source Link

Document

Register a callback to be invoked when this view is clicked and held.

Usage

From source file:de.grobox.liberario.DirectionsFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // remember view for UI changes when fragment is not active
    mView = inflater.inflate(R.layout.fragment_directions, container, false);
    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

    checkPreferences();//ww w . j av  a2s  .c  o  m

    setFromUI();
    setToUI();

    // timeView
    final Button timeView = (Button) mView.findViewById(R.id.timeView);
    timeView.setText(DateUtils.getcurrentTime(getActivity()));
    timeView.setTag(Calendar.getInstance());
    timeView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showTimePickerDialog();
        }
    });

    // set current time on long click
    timeView.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            timeView.setText(DateUtils.getcurrentTime(getActivity()));
            timeView.setTag(Calendar.getInstance());
            return true;
        }
    });

    Button plus10Button = (Button) mView.findViewById(R.id.plus15Button);
    plus10Button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            addToTime(15);
        }
    });

    // dateView
    final Button dateView = (Button) mView.findViewById(R.id.dateView);
    dateView.setText(DateUtils.getcurrentDate(getActivity()));
    dateView.setTag(Calendar.getInstance());
    dateView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showDatePickerDialog();
        }
    });

    // set current date on long click
    dateView.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            dateView.setText(DateUtils.getcurrentDate(getActivity()));
            dateView.setTag(Calendar.getInstance());
            return true;
        }
    });

    // Trip Date Type Spinner (departure or arrival)
    final TextView dateType = (TextView) mView.findViewById(R.id.dateType);
    dateType.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (dateType.getText().equals(getString(R.string.trip_dep))) {
                dateType.setText(getString(R.string.trip_arr));
            } else {
                dateType.setText(getString(R.string.trip_dep));
            }
        }
    });

    // Products
    final ViewGroup productsLayout = (ViewGroup) mView.findViewById(R.id.productsLayout);
    for (int i = 0; i < productsLayout.getChildCount(); ++i) {
        final ImageView productView = (ImageView) productsLayout.getChildAt(i);
        final Product product = Product.fromCode(productView.getTag().toString().charAt(0));

        // make inactive products gray
        if (mProducts.contains(product)) {
            productView.getDrawable().setColorFilter(null);
        } else {
            productView.getDrawable().setColorFilter(getResources().getColor(R.color.highlight),
                    PorterDuff.Mode.SRC_ATOP);
        }

        // handle click on product icon
        productView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (mProducts.contains(product)) {
                    productView.getDrawable().setColorFilter(getResources().getColor(R.color.highlight),
                            PorterDuff.Mode.SRC_ATOP);
                    mProducts.remove(product);
                    Toast.makeText(v.getContext(), LiberarioUtils.productToString(v.getContext(), product),
                            Toast.LENGTH_SHORT).show();
                } else {
                    productView.getDrawable().setColorFilter(null);
                    mProducts.add(product);
                    Toast.makeText(v.getContext(), LiberarioUtils.productToString(v.getContext(), product),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

        // handle long click on product icon by showing product name
        productView.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(view.getContext(), LiberarioUtils.productToString(view.getContext(), product),
                        Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    if (!Preferences.getPref(getActivity(), Preferences.SHOW_ADV_DIRECTIONS)) {
        (mView.findViewById(R.id.productsScrollView)).setVisibility(View.GONE);
    }

    Button searchButton = (Button) mView.findViewById(R.id.searchButton);
    searchButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            NetworkProvider np = NetworkProviderFactory.provider(Preferences.getNetworkId(getActivity()));
            if (!np.hasCapabilities(NetworkProvider.Capability.TRIPS)) {
                Toast.makeText(v.getContext(), v.getContext().getString(R.string.error_no_trips_capability),
                        Toast.LENGTH_SHORT).show();
                return;
            }

            AsyncQueryTripsTask query_trips = new AsyncQueryTripsTask(v.getContext());

            // check and set to location
            if (checkLocation(FavLocation.LOC_TYPE.TO)) {
                query_trips.setTo(getLocation(FavLocation.LOC_TYPE.TO));
            } else {
                Toast.makeText(getActivity(), getResources().getString(R.string.error_invalid_to),
                        Toast.LENGTH_SHORT).show();
                return;
            }

            // check and set from location
            if (mGpsPressed) {
                if (getLocation(FavLocation.LOC_TYPE.FROM) != null) {
                    query_trips.setFrom(getLocation(FavLocation.LOC_TYPE.FROM));
                } else {
                    mAfterGpsTask = query_trips;

                    pd = new ProgressDialog(getActivity());
                    pd.setMessage(getResources().getString(R.string.stations_searching_position));
                    pd.setCancelable(false);
                    pd.setIndeterminate(true);
                    pd.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    mAfterGpsTask = null;
                                    dialog.dismiss();
                                }
                            });
                    pd.show();
                }
            } else {
                if (checkLocation(FavLocation.LOC_TYPE.FROM)) {
                    query_trips.setFrom(getLocation(FavLocation.LOC_TYPE.FROM));
                } else {
                    Toast.makeText(getActivity(), getString(R.string.error_invalid_from), Toast.LENGTH_SHORT)
                            .show();
                    return;
                }
            }

            // remember trip if not from GPS
            if (!mGpsPressed) {
                FavDB.updateFavTrip(getActivity(), new FavTrip(getLocation(FavLocation.LOC_TYPE.FROM),
                        getLocation(FavLocation.LOC_TYPE.TO)));
            }

            // set date
            query_trips.setDate(DateUtils.mergeDateTime(getActivity(), dateView.getText(), timeView.getText()));

            // set departure to true of first item is selected in spinner
            query_trips.setDeparture(dateType.getText().equals(getString(R.string.trip_dep)));

            // set products
            query_trips.setProducts(mProducts);

            // don't execute if we still have to wait for GPS position
            if (mAfterGpsTask != null)
                return;

            query_trips.execute();
        }
    });

    return mView;
}

From source file:de.uulm.graphicalpasswords.openuyi.UYICreatePasswordActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_uyi_create_password);
    // Show the Up button in the action bar.
    setupActionBar();//from  ww w. j  a v a 2 s  .co m

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    length = Integer.parseInt(sharedPref.getString("uyi_length", "10"));

    originalViews = new ImageView[length];
    distortedViews = new ImageView[length];
    selectedPictures = new Picture[length];

    Bundle bundle = new Bundle();
    bundle.putInt("length", length);
    DialogFragment intro = new IntroDialogFragment();
    intro.setArguments(bundle);
    intro.show(getFragmentManager(), "intro");

    vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);

    Arrays.fill(selectedPictures, null);

    gallery = (Gallery) findViewById(R.id.uyi_gallery_originals);
    gallery.setAdapter(new UYIImageAdapter(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            ImageView viewOriginal = new ImageView(UYICreatePasswordActivity.this);
            ImageView viewDistorted = new ImageView(UYICreatePasswordActivity.this);

            int i = 0;
            for (; i < selectedPictures.length; i++) {
                if (i == selectedPictures.length - 1 && selectedPictures[i] != null) {
                    removePicture(i);
                }
                if (selectedPictures[i] == null || i == selectedPictures.length - 1) {
                    viewOriginal = originalViews[i];
                    viewDistorted = distortedViews[i];
                    selectedPictures[i] = ((UYIImageAdapter) parent.getAdapter()).getPicture(position);
                    vibrator.vibrate(100);

                    ScrollView sv = (ScrollView) findViewById(R.id.uyi_choosepi_scrollview);
                    int height = originalViews[0].getMeasuredHeight();
                    sv.scrollTo(0, (i * height) - 200);

                    break;
                }
            }

            int originalImageResource = ((UYIImageAdapter) parent.getAdapter()).getImageResource(position);
            viewOriginal.setImageResource(originalImageResource);
            viewDistorted.setImageResource(
                    ((UYIImageAdapter) parent.getAdapter()).getDistortedImageResource(position));
            ((UYIImageAdapter) parent.getAdapter()).removePicture(position);

            OnLongClickListener listener = new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    vibrator.vibrate(200);
                    int viewid = v.getId();
                    int index = -1;

                    for (int i = 0; i < originalViews.length; i++) {
                        if (originalViews[i].getId() == viewid) {
                            index = i;
                            break;
                        } else if (distortedViews[i].getId() == viewid) {
                            index = i;
                            break;
                        }
                    }
                    Bundle bundle = new Bundle();
                    bundle.putInt("index", index);
                    DialogFragment dialog = new DeleteImageDialogFragment();
                    dialog.setArguments(bundle);
                    dialog.show(getFragmentManager(), "delete");
                    return false;
                }
            };

            viewOriginal.setOnLongClickListener(listener);
            viewDistorted.setOnLongClickListener(listener);

            // Check
            int count = 0;
            for (int j = 0; j < selectedPictures.length; j++) {
                if (selectedPictures[j] != null) {
                    count++;
                }
            }
            if (count == selectedPictures.length) {
                findViewById(R.id.uyi_save).setClickable(true);
                findViewById(R.id.uyi_save).setEnabled(true);
            }
        }
    });

    table = (TableLayout) findViewById(R.id.uyi_choosepi_tablelayout);
    LayoutParams params = new LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,
            android.widget.TableRow.LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER;
    for (int i = 0; i < length; i++) {
        TableRow row = new TableRow(table.getContext());

        originalViews[i] = new ImageView(row.getContext());
        originalViews[i].setAdjustViewBounds(true);
        originalViews[i].setScaleType(ScaleType.FIT_XY);
        originalViews[i].setPadding(3, 3, 3, 3);
        originalViews[i].setImageResource(R.drawable.oempty);
        originalViews[i].setId(100 + i);

        distortedViews[i] = new ImageView(row.getContext());
        distortedViews[i].setAdjustViewBounds(true);
        distortedViews[i].setScaleType(ScaleType.FIT_XY);
        distortedViews[i].setPadding(3, 3, 3, 3);
        distortedViews[i].setImageResource(R.drawable.wempty);
        distortedViews[i].setId(1000 + i);

        ImageView arrow = new ImageView(row.getContext());
        arrow.setAdjustViewBounds(true);
        arrow.setScaleType(ScaleType.FIT_XY);
        arrow.setMaxWidth(80);
        arrow.setPadding(3, 3, 3, 3);
        arrow.setImageResource(R.drawable.arrow_active);
        arrow.setLayoutParams(params);

        row.addView(originalViews[i]);
        row.addView(arrow);
        row.addView(distortedViews[i]);
        table.addView(row);
    }
}