Add view on top of MediaController
Asked Answered
S

2

4

MediaController always shown on top of all views in the screen and do not pass clicks to below views until it is hidden, my issue is:

How to add button view on top of MediaController so it handle click events ? OR How to pass click events to below view?

Sharecropper answered 18/8, 2012 at 20:43 Comment(2)
for the 58 percent i accepted because it solved the issue .. for others that just helped i clicked up ... i cannot click accepted and next one searching for the issue think that answer solve the issue and problem from him :)Sharecropper
Oh, ok then, I didn't realize. That's a good point :)Phooey
P
2

Try overriding MediaController.dispatchTouchEvent()

It suits your task better, see my answer here for a detailed explanation why.

The code will go something like this:

public class MyMediaController extends MediaController {
    ...
    private WeakReference<Button> mButton;

    // TODO You'll have to call this whenever the layout might have been rebuilt
    // i.e. in onCreate() of your activity (or in onConfigurationChanged() if you
    // handle configuration changes yourself)
    public void setButton(Button button) {
        mButton = new WeakReference<Button>(button);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int iX = (int) event.getX();
        int iY = (int) event.getY();

        Button button = mButton.get();
        if (button != null) {
            Rect buttonHitRect = new Rect();
            button.getHitRect(buttonHitRect);
            if (buttonHitRect != null && buttonHitRect.contains(iX, iY)) {
                // user hit the button, dispatch event to the button
                button.dispatchTouchEvent(event);
                return true;
            }
        }
        // button did not get hit, pass the touch through
        return super.dispatchTouchEvent(event)
    }
}

Try this, let me know how it goes.

UPD Theres's a very similar question on this, it may have a better solution: Android MediaController intercepts all other touch events

Phooey answered 28/8, 2012 at 10:21 Comment(4)
i wil check it and back to you.Sharecropper
Hey Alex, did it work? Also, check out the linked question, the topic's exactly the same.Phooey
i have checked your update .... when read the MediaController from xml it through null exceptionSharecropper
Well, I can't realy tell without the stacktrace but it's most likely because you didn't assign the weak ref there. You have to find the media controller and the button in onCreate() of your activity and call setButton() on it. If it's not it, post the exception and I'll try to help you.Phooey
S
1

When a view is overlapping another view then the hidden view will not get any touch event as the view on top consumes the touch event. If you want to the hidden view to receive the touch event then you have to manually pass the touch event from the top view to the hidden view. Here there are two possibilities:

  • You want the touch event to be shared by both the view: In this case after passing the touch event to the hidden view indicate android that touch event has not been consumed by returning false from the onTouch() method of the top view.
  • You want the touch event to be handled by only the hidden view: In this case after passing the touch event to the hidden view indicate android that the touch event been consumed by returning true from the onTouch() method of the top view.

Here is a sample code for this:

btn.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch (View v, MotionEvent event)
        {
            list.onTouchEvent(event);
            return true;       // return true if touch event is consumed else return false
        }
    });

XML for this:

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

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000" >
    </ListView>

    <Button
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#88cc0000"
        android:layout_alignParentTop="true"
        android:onClick="showMe" />
</RelativeLayout>

Here Button is hiding the list view still the list will scroll as the touch event is passed to the below layout.
I hope this will help. :)

Stone answered 21/8, 2012 at 22:9 Comment(1)
Thanks it is does not work for media controller case. when medi controller is show any outer click will handled by it but do not call ontouchmethod.Sharecropper

© 2022 - 2024 — McMap. All rights reserved.