Reputation: 5401
I have a RelativeLayout
and an ImageView
.
The layout is given below -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black" >
<ImageView
android:id="@+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
</RelativeLayout>
The Activity
code is -
public class MainActivity extends Activity implements OnClickListener{
private RelativeLayout rlMain;
private ImageView ivIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rlMain = (RelativeLayout) findViewById(R.id.rlMain);
ivIcon = (ImageView) findViewById(R.id.ivIcon);
rlMain.setOnClickListener(this);
ivIcon.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rlMain:
Toast.makeText(this, "Relative Layout clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.ivIcon:
Toast.makeText(this, "Image View clicked", Toast.LENGTH_SHORT).show();
break;
}
}
}
I have applied the on click listener for both the RelativeLayout
(parent) and the ImageView
(child).
While clicking on the relative layout - it is handled by the relative layout click handler.(This seems to be correct).
While clicking on the Imagview - It is handled by the imageview. (Here is the confusion).
Should the click on the imageview get handled by both the parent(relative layout) and child (imageview)? What is the logic if only the child view is handling the click?
Upvotes: 3
Views: 10596
Reputation: 300
getGlobalVisibleRect()
or getLocalVisibleRect()
to detect view's position on your needs.MotionEvent.ACTION_DOWN
to get only first touch in the case of user make slide action on the screen.The Kotlin Code:
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean { if(ev?.action == MotionEvent.ACTION_DOWN) { val pointX = ev.x val pointY = ev.y val locationIvIcon = Rect() if(iv_icon.getGlobalVisibleRect(locationIvIcon)) { if(isInChildView(pointX, pointY, locationSendGiftPopup)) { // Do action for ImageView } else { // Do action for RelativeLayout } } } return super.dispatchTouchEvent(ev) } private fun isInChildView(x: Float?, y: Float?, location: Rect): Boolean { x?.let { y?.let { return x >= location.left && x <= location.right && y >= location.top && y <= location.bottom } } return false }
Upvotes: 0
Reputation: 123
A clickEvent will be delivered to the lowest child element in the layout hierarchy. If this element does not have an onClick behaviour it will pass the event up to its parent until the event gets handled.
Therefore you can treat the LinearLayout as one single block for your onClick behaviour. If you create another clickable element inside the layout be sure to make it big enough to reduce the chance of the user missing the correct item.
Source: Does making parent clickable make all child element clickable as well?
Upvotes: 3
Reputation: 2106
Normally in cases where the image to be clicked is very small in dimension the imageView is kept inside a layout which is given some padding, then the click of the layout is handled. This gives enough area on screen for the user to click on
Upvotes: 0