Java how to feel that an icon is pressed in android

Java how to feel that an icon is pressed in android

To detect when an icon is pressed in Android, you can use the OnClickListener interface and attach it to the icon view.

Here's an example of how to detect when an icon is pressed in Android:

ImageView icon = findViewById(R.id.icon);

icon.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    // Code to execute when the icon is clicked
  }
});
Sou‮ecr‬:www.lautturi.com

In the above example, the setOnClickListener() method is used to attach an OnClickListener to the icon view. The onClick() method of the OnClickListener is called when the icon is clicked.

You can also use the setOnLongClickListener() method to detect a long press on the icon.

Keep in mind that you need to import the necessary classes and specify the correct view ID in the findViewById() method.

Alternatively, you can use the android:onClick attribute in the layout XML file to specify a method to be called when the icon is clicked.

For example:

<ImageView
  android:id="@+id/icon"
  android:onClick="iconClicked"
  ... />

In this case, you need to define a method in your activity that has the same name as the value of the android:onClick attribute and takes a View argument. The method will be called when the icon is clicked.

public void iconClicked(View view) {
  // Code to execute when the icon is clicked
}
Created Time:2017-11-03 22:21:06  Author:lautturi