0

This Question Is Pretty Silly to ask but out, but i Would like to know how it Works?

I had a Relative Layout with 2 ImageViews as Child having separate clickListner instances. One above Another, of same Size and Attributes.

Overlaps Each other. Both having Different images. Question is When i click on one image both ImageView Click listners are Called.

Or if i disable the Click on ImageView Top, The ImageView Below Still Works, I was Clicking on Image View Above though. How it is I'ts Getting callback from both.

I Just Want to know How it works? not The code, i do not have any issue writing code for clickListners Whether only one Working or Both.

<RelativeLayout

----
---
>
<ImageView
---
---<!--Child 1-->
<ImageView
---
---<!--Child 2-->



<RelativeLayout/>
Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
Jayant Arora
  • 1,241
  • 2
  • 15
  • 24

3 Answers3

0

Taken from here: if you are working with just one clicklistener, you can do:

View.OnClickListener myOnlyhandler = new View.OnClickListener() {
  public void onClick(View v) {
      switch(v.getId()) {
        case R.id.b1:
          // it was the first button
          break;
        case R.id.b2:
          // it was the second button
          break;
      }
  }
}
Community
  • 1
  • 1
Vitaliy A
  • 3,651
  • 1
  • 32
  • 34
0

Use ImageButton

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/content"
    android:src="@drawable/ic_launcher" />

 </LinearLayout>

Activity class

public class MainActivity extends Activity {

ImageButton imgButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addButtonListener();
}

public void addButtonListener() {

    imgButton = (ImageButton) findViewById(R.id.imageButton);
    imgButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
           Toast.makeText(MainActivity.this,"ImageButton is working!", Toast.LENGTH_SHORT).show();
        }
    });
}
}
MSA
  • 2,502
  • 2
  • 22
  • 35
0

Give this to your parent layout android:context="Yourclasshere" then give this to your image view android:onclick="onclick"

and then implement the on click listener or make the method like Vitly A make above

Hasnain
  • 274
  • 2
  • 14