0

I'm trying to subclass MediaController in order to make it fully customizable (I'm not going with vidtry approach because I have some legacy code that I can't change).

My code looks roughly like this:

mc_layout.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ...>

        // Some buttons

    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ... >

        // Some buttons

    </LinearLayout>

mc_subclass.java:

public class MCSubclass extends MediaController {

private View mRoot;

GestureDetector mGestureDetector = new GestureDetector(getContext(), new SimpleOnGestureListener() {

    @Override
    public boolean onDown(MotionEvent e) {
     // Do something.
        return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
       // Do something.
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        // Do something.
        return false;
    }
});

public MCSubclass(final Context context) {
    super(context);
}

public boolean onTouchEvent(final MotionEvent event) {
// NEVER CALLED!
    return mGestureDetector.onTouchEvent(event);
}

public final void setAnchorView(final View view) {
    super.setAnchorView(view);
    FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
    );

    removeAllViews();
    View v = makeControllerView();
    addView(v, frameParams);
}

private View makeControllerView() {
    LayoutInflater inflate = (LayoutInflater) getContext().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);

    mRoot = inflate.inflate(R.layout.mc_layout, null);
    initControllerView(mRoot);
    return mRoot;
}

private void initControllerView(View v) {
    mPlayButton = (ImageButton) v.findViewById(R.id.playBtn);
    if (mPlayButton != null) {
        mPlayButton.requestFocus();
        mPlayButton.setOnClickListener(mOnClickListener);
    }
// Init other views...
}

}

Now, I got all controls visible and they're all responding to their's click/touch actions but when I click/touch the MC window instead of invoking an overridden onTouchEvent as I expected the MediaController's onTouch of a decor window got called as it can be seen in the callstack:

MediaController$1.onTouch(View, MotionEvent) line: 149  
PhoneWindow$DecorView(View).dispatchTouchEvent(MotionEvent) line: 3762  
PhoneWindow$DecorView(ViewGroup).dispatchTouchEvent(MotionEvent) line: 897  
PhoneWindow$DecorView.dispatchTouchEvent(MotionEvent) line: 1862    
ViewRoot.handleMessage(Message) line: 1809  
ViewRoot(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 123 

The question is do I need to replace the decor window touch listener or there is a better way and I'm missing something?

p.s. I'm working with lv. 9 API.

Thanks!

sinek
  • 2,458
  • 3
  • 33
  • 55

1 Answers1

1

I found it! The problem is in using FrameLayout which can display only a single item and whose size is the size of a largest child. So in my case it was not large enough to cover the whole screen as I expected. As the result, the decor window ended up handling the touch event which make perfect sense.

Anyway the working layout now looks like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...>

        // Some buttons

    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ... >

        // Some buttons

    </LinearLayout>
    ....
</RelativeLayout>   

Hope this will help one day to someone:)

sinek
  • 2,458
  • 3
  • 33
  • 55