title: Android ImageView is raising OnClick event when is hidden
body: <p>I have couple questions about android views. I'm newbie so if my question seems ridiculous please give me right hint :)
  So I'm trying to make memory game. Here is my code of mainLayout. </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
   &lt;RelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/mainPanel"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android.background="Color.TRANSPARENT"
      android:gravity="top"
      android:orientation="vertical" &gt;
  
      &lt;LinearLayout
          android:id="@+id/top_panel"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="2dp" &gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Blaaaaaa1" /&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Blaaaaaa2" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;EditText
          android:id="@+id/editText1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:ems="3"
          android:inputType="textPassword"
          android:text="dsdsdsd"
          android:visibility="invisible" &gt;
  
          &lt;requestFocus /&gt;
      &lt;/EditText&gt;
  
      &lt;TableLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/top_panel"
          android:paddingBottom="50dp"
          android:paddingTop="5dp"
          android:weightSum="5" &gt;
  
          &lt;TableRow
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:weightSum="5" &gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_a1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_a2"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_a3"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_a4"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_a5"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
          &lt;/TableRow&gt;
  
          &lt;TableRow
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:weightSum="5" &gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_b1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_b2"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_b3"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_b4"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_b5"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
          &lt;/TableRow&gt;
  
          &lt;TableRow
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:weightSum="5" &gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_c1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_c2"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_c3"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_c4"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_c5"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
          &lt;/TableRow&gt;
  
          &lt;TableRow
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:weightSum="5" &gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_d1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_d2"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_d3"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_d4"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_d5"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
          &lt;/TableRow&gt;
  
          &lt;TableRow
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:weightSum="5" &gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_e1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_e2"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_e3"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_e4"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:orientation="vertical" &gt;
  
                  &lt;ImageView
                      android:id="@+id/pic_e5"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" /&gt;
              &lt;/LinearLayout&gt;
          &lt;/TableRow&gt;
  
          &lt;TableRow
              android:id="@+id/tableRow1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" &gt;
          &lt;/TableRow&gt;
      &lt;/TableLayout&gt;
  
      &lt;ImageView
          android:id="@+id/imageView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_centerVertical="true"
          android:src="@drawable/pic9214" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and here is code of mainActivity.</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MainActivity extends Activity {
      private OnClickListener paSharedOnClickListener = new OnClickListener();
      private ImageView paPreviewView ;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_main);
          getWindow().setBackgroundDrawableResource(R.drawable.old_paper_r);
  
  
  
          ImageView img; 
  
          img = (ImageView) MainActivity.this.findViewById(R.id.imageView1) ;
          img.setVisibility(-1);
          this.paPreviewView = img;
  
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_a1);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_a2);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_a3);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_a4);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_a5);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_b1);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_b2);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_b3);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_b4);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_b5);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_c1);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_c2);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_c3);
          img.setVisibility(-1);
          img = (ImageView) findViewById(R.id.pic_c4);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_c5);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_d1);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_d2);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_d3);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_d4);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_d5);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_e1);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_e2);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_e3);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_e4);
          img.setOnClickListener(paSharedOnClickListener);
          img = (ImageView) findViewById(R.id.pic_e5);
          img.setOnClickListener(paSharedOnClickListener);
  
      }
  
  
      private class OnClickListener implements View.OnClickListener
      {
          private boolean paIsPreviewShowed = false;
  
          public void onClick(View arg0) 
          {
  
              ImageView img  = (ImageView)arg0;
  
              Log.v("Click", String.format("ID %s %s", img.getId() , (ImageView) MainActivity.this.findViewById(R.id.imageView1) == img ) );
  
  
              if (!this.paIsPreviewShowed)
              {
  
                  this.paIsPreviewShowed = true;
  
                  Animation fadeIn = new AlphaAnimation( 0, 1);
                  fadeIn.setDuration(150);
  
  
                  AnimationSet animationSet = new AnimationSet(true);
  
                  ScaleAnimation scaleAnim = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
                  scaleAnim.setInterpolator(new LinearInterpolator());
                  scaleAnim.setRepeatCount(Animation.ZORDER_NORMAL);
                  scaleAnim.setDuration(250);
  
                  TranslateAnimation move = new TranslateAnimation(0, 0, 500,0);
                  move.setDuration(250);
  
  
                  animationSet.addAnimation(fadeIn);
                  animationSet.addAnimation(scaleAnim);
                  animationSet.setFillAfter(true);
  
  
  
  
  
                  Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.pic9012);
                  if  (paPreviewView.getParent()==null )
                  {
                      RelativeLayout top_panel  = (RelativeLayout) MainActivity.this.findViewById(R.id.mainPanel);
                      top_panel.addView(paPreviewView);
                  }
                  paPreviewView.setImageBitmap(bMap);
                  paPreviewView.startAnimation(animationSet);
  
  
              }else
              {
  
                  Animation fadeOut = new AlphaAnimation( 1, 0);
                  fadeOut.setFillAfter(true);
                  fadeOut.setDuration(250);
                  paPreviewView.startAnimation(fadeOut);
                  this.paIsPreviewShowed = false;
  
                  ((RelativeLayout) paPreviewView.getParent()).removeView(paPreviewView);
              }
  
                  }
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
  }
  </code></pre>
  
  <p>Ok so, in first step after create Activiti I'm setting up OnClickListener to every card (pic_a1, pic_a2 ... pic_e5) and visiblity of ImageView paPreviewView to invisible (where i wanna show image of selected card). And then when i clicked to pic_a1 is paPreviewView showed. This is ok. 
  My questions are.</p>
  
  <p>1) In my early version of this example I hid paPreviewView ImageView by calling</p>
  
  <pre class="lang-java prettyprint-override"><code>paPreviewView.setVisibility(View.INVISIBLE);
  </code></pre>
  
  <p>or</p>
  
  <pre class="lang-java prettyprint-override"><code>paPreviewView.setVisibility(View.GONE);
  </code></pre>
  
  <p>but it didn't work properly. Because method OnClickListener.onClick was called only from hidden paPreviewView ImageView. So i tried to remove OnClickListener from paPreviewView but it didn't help. Only working solution is remove (for hidding ) and add (for showing) paPreviewView from /to mainPanel.</p>
  
  <p>My question is why? Why is paPreviewView raising onClick event when paPreviewView is hidden?</p>
  
  <p>2) And my second question is. Is better solution  than I made for this example (I mean card game) ? </p>
  
  <p>Thanks a lot for any idea :)</p>