Clicking
View.OnClickListener and View.OnLongClickListener define interfaces for callbacks to be invoked when a view is clicked.
Callbacks can be assigned programatically for any view
imgView.setOnClickListener{
// do something
}
imgView.setOnLongClickListener {
// do something
true
}
imgView.setOnClickListener(view -> {
// ... do something
});
imgView.setOnLongClickListener(view ->{
// ... do something
return true;
});
Click callback can be assigned from xml layout file. To do this activity must have method that handle click, for example:
fun onClickImage(v: View?) {/* ... */}
void onClickImage(View v){ /* ... */}
Then you can use onClick attribute.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imgMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/flower_n_2"
android:scaleType="centerCrop"
android:onClick="onClickImage"
>
</ImageView>
</FrameLayout>