Kalpesh Lakhani
Kalpesh Lakhani

Reputation: 993

how to disable dispatch touch event in some portion of screen

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.

Upvotes: 3

Views: 7847

Answers (4)

kaushal trivedi
kaushal trivedi

Reputation: 3443

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

Upvotes: 0

Nermeen
Nermeen

Reputation: 15973

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);
}

Upvotes: 8

Luis
Luis

Reputation: 12048

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.

Upvotes: 2

Chet
Chet

Reputation: 1225

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

Upvotes: 0

Related Questions