ImageView imageView = findViewById(R.id.image_view); imageView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Toast.makeText(getApplicationContext(), "Long clicked", Toast.LENGTH_SHORT).show(); return true; } });
public class MainActivity extends AppCompatActivity implements View.OnLongClickListener { ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.image_view); imageView.setOnLongClickListener(this); } @Override public boolean onLongClick(View v) { imageView.setImageResource(R.drawable.new_image); return true; } }This example sets a long click listener on an ImageView widget using the Activity as the listener. When the image view is long clicked, it changes the image resource to a new image. Package library: android.widget.