3

I want to disable dispatch touch for some area for your understanding here is my screen.

enter image description here

You can see Header,Footer and Mapview in image but when I clicked on location button(right side in header), My map is also getting notified (like it got touched) which I don't want. I want that only onClick event of button should be clicked not dispatch event.

Here is my XML:

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

<!-- include title bar for all screen -->

<include
    android:id="@+id/include1"
    layout="@layout/titlebar_layout"
    android:layout_gravity="top" />



<FrameLayout
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.88" 
    >

    <com.google.android.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.63"
        android:apiKey="0VkXbAOFvAq7b6uaGHSmnS2a2VosPxoS6ceHY_g"
        android:clickable="true" >
    </com.google.android.maps.MapView>


</FrameLayout>

<include
    android:id="@+id/include2"
    android:layout_gravity="bottom"
    android:layout_marginBottom="38dp"
    layout="@layout/bottom_layout" />

</LinearLayout>

Any help will be greatly appreciated.

Kalpesh Lakhani
  • 1,003
  • 14
  • 28

4 Answers4

8

You can override the dispatchTouchEvent, and check where the user has touched the screen..

  @Override
public boolean dispatchTouchEvent(MotionEvent ev) {     
    //first check if the location button should handle the touch event
    if(locBtn != null) {
        int[] pos = new int[2];
        locBtn.getLocationOnScreen(pos);
        if(ev.getY() <= (pos[1] + locBtn.getHeight()) && ev.getX() > pos[0]) //location button event
            return locBtn.onTouchEvent(ev);
    }
        return super.dispatchTouchEvent(ev);
}
Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • hey thanks for answer..i'll be back soon to accept your answer if this works – Kalpesh Lakhani Oct 11 '12 at 07:13
  • hey nunu your answer is really helpful for me but still i cant get button location what is pos[1] ?? if i do pos[0] there then i got constantly button event even if i touch anywhere else.. plz help me – Kalpesh Lakhani Oct 12 '12 at 08:18
  • 1
    http://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[]), pos[0] contains the x of the view, pos[1] contains the y.. try if(ev.getY() <= (pos[1] + locBtn.getHeight()) && ev.getX() > pos[0]) – Nermeen Oct 12 '12 at 08:31
  • OMG its working SOflow please allow me to upvote this answer 10 times..it worth to me really thanks a lot @Nunu – Kalpesh Lakhani Oct 12 '12 at 08:54
  • Hats off to such a great attitude – Kalpesh Lakhani Oct 12 '12 at 09:11
2

The reason why MapView is receiving the dispatchTouchEvent() events is that Android starts dispacthing the the events for the last added view, and if it's not handled there, then it's dispatched to the last the view added before that one, and so on ...

In you xml layout, you first add Header, then MapView and for last Footer. So in your case, the the events are first dispatched to Footer, then to MapView, and for last to the Header.

You have to option to solve this:

  • The simplest is to reorder the items in your layout, starting with mapview and then adding all the others. You may need to use relative layout to be able to position them correctly. With this MapView will not see events that are handle by the button.
  • Keep your layout and let MapView receive the events. MapView will need to test if event should be handled by him or ignored. For that you can use the code sugested by @Nunu, which should work. Maybe you need to add to the button Y coordinate the button height.

good luck.

Luis
  • 11,978
  • 3
  • 27
  • 35
  • +1 no one can stop me to press Upvote for such a good explaination..thanks a lot – Kalpesh Lakhani Oct 11 '12 at 11:15
  • i'll be back soon to appreciate your answer..:):) – Kalpesh Lakhani Oct 11 '12 at 11:16
  • @Luis..thanks again man i following your second option (also suggested by @Nunu) but not worked..:( what do you mean by adding y co-ordinate to button height?? thanks in advance – Kalpesh Lakhani Oct 12 '12 at 08:20
  • When you call `getLocationOnScreen()` you get the Y coordinate of the button top. When clicking the button, the user will be clicking the area between the top of button and button. You can find the botton y coordinate adding the button height. – Luis Oct 12 '12 at 10:20
0

Use relative layout with Map View at bottom and Button to clicked at top.. and add click event for the button

Chet
  • 1,205
  • 1
  • 12
  • 20
0

use view.bringToFront() to get the view places above other views.

kaushal trivedi
  • 3,405
  • 3
  • 29
  • 47