title: Detecting if rectangles (buttons) overlap on x or y axis
body: <p>I am currently trying to develop a prototype in android where the user is able to drag 4 separate buttons around the screen.</p>
  
  <p>The trouble I am having is with collision detection. If one of the buttons is alongside another, for example, only movement along the Y axis should be permitted. Similarly, if one of the buttons is touching the top or bottom of another button, only movement along the X axis should be allowed.</p>
  
  <p>Something like </p>
  
  <pre><code>for (TouchButton t:myButtons)
  {
    if(!(t.equals(b)))
    {
     if((b.getY() &gt;= t.getY() &amp;&amp; (b.getY() &lt;= (t.getY() + t.getMeasuredHeight()))))
     {
         if((b.getX() &gt;= t.getX() &amp;&amp; (b.getX() &lt;= (t.getX() + t.getMeasuredWidth()))))
         {
             //dont move
         }
     }
  }
  </code></pre>
  
  <p>should be able to detect if the buttons are touching across both axis? But how do I determine if it should then be possible to slide up or across the object?</p>
  
  <p><img src="https://i.stack.imgur.com/SIwlT.png" alt="Example"></p>