1. Download our Official Android App: Forums for Android!

Apps MotionEvent and relative layout

Discussion in 'Android Development' started by spikey100, Nov 24, 2011.

  1. spikey100

    spikey100 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    7
    Joined:
    Nov 4, 2011

    Nov 4, 2011
    7
    0
    5
    I have a relative layout built from XML.
    I am capturing the onTouchEvent.
    When I touch the screen anywhere except over the TableLayout or ImageButton
    the MotionEvent.ACTION_DOWN and MotionEvent.ACTION_MOVE
    events work fine.
    Any suggestions as to how to make the onTouchEvent() work on the TableLayout
    and ImageButton views?

    The purpose of the exercise was to have a drag method working over the relative layout,
    nothing I have read suggests it is possible unfortunately.
    Maybe manipulating the layout_margin values?
    I was contemplating a transparent canvas overlay but haven't figured that out yet.

    Code snippets below

    XML
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="left"
    android:background="@drawable/yellowbackground">
    <ImageButton
    android:id="@+id/back_butt"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    android:layout_width="40dp"
    android:layout_height="36dp"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:clickable = "true"
    android:eek:nClick="additionClickHandler"
    android:src="@drawable/back_button"
    android:background="@null"
    ></ImageButton>
    <ImageView
    android:id="@+id/r2c2"
    android:layout_centerInParent="true"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:src="@drawable/four"
    android:background="@null"
    >
    </ImageView>
    .....more image views here
    <TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_alignParentTop="true"
    >
    <TableRow>
    <ImageView
    android:id="@+id/imageB1"
    android:maxWidth="47dp"
    android:padding="1dp"
    android:layout_width="wrap_content"
    android:layout_height="47dp"
    android:clickable = "true"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:src="@drawable/one"
    android:background="@drawable/buttonbg"
    ></ImageView>
    <ImageButton
    android:id="@+id/imageB2"
    android:maxWidth="47dp"
    ..........
    android:src="@drawable/two"
    android:background="@drawable/buttonbg"
    ></ImageButton>
    <ImageButton
    android:id="@+id/imageB3"
    android:maxWidth="47dp"
    ..............
    android:src="@drawable/three"
    android:background="@drawable/buttonbg"
    ></ImageButton>
    </TableRow>
    </TableLayout>

    </RelativeLayout>

    JAVA

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
    final float x = ev.getX();
    final float y = ev.getY();

    // Remember where we started
    mLastTouchX = x;
    mLastTouchY = y;
    Log.d(TAG, "Coords: x=" + x + ",y=" + y);
    break;
    }

    case MotionEvent.ACTION_MOVE: {
    final float x = ev.getX();
    final float y = ev.getY();
    Log.d(TAG, "moved to: x=" + x + ",y=" + y);

    Any help would be great.
     

    Advertisement

  2. spikey100

    spikey100 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    7
    Joined:
    Nov 4, 2011

    Nov 4, 2011
    7
    0
    5
  3. JamTheMan

    JamTheMan Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    136
    Joined:
    Dec 16, 2010

    It might have something to do with the way Android handles touch input. Simply put, some view can consume the event, without passing it on. This becomes a problem if you have different views on top of each other, that all need to register and handle the touch event.

    First thing you should try is to explicitly handle all touches in all the views that get touches and simply implements their onTouch method to return false, which means that the view does not want to handle that specific touch event id, and that it should be passed on. Returning true, means that the view consumes the event - handles it and does not pass it on.

    Try that, for a start...
     

Share This Page

Loading...