Example usage for android.view MotionEvent getAction

List of usage examples for android.view MotionEvent getAction

Introduction

In this page you can find the example usage for android.view MotionEvent getAction.

Prototype

public final int getAction() 

Source Link

Document

Return the kind of action being performed.

Usage

From source file:com.android.screenspeak.contextmenu.RadialMenuView.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_HOVER_ENTER:
        // Fall-through to movement events.
        onEnter(event.getX(), event.getY());
    case MotionEvent.ACTION_MOVE:
    case MotionEvent.ACTION_HOVER_MOVE:
        onMove(event.getX(), event.getY());
        break;/*w  w w.j a v a2s.  c  o  m*/
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_HOVER_EXIT:
        onUp(event.getX(), event.getY());
        break;
    default:
        // Don't handle other types of events.
        return false;
    }

    mHandler.onTouch(this, event);

    return true;
}

From source file:com.affectiva.affdexme.MainActivity.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        setMenuVisible(!isMenuVisible);/* w w  w  .j  a  v a  2 s  .co  m*/
    }
    return false;
}

From source file:com.scigames.slidegame.Registration1UserNameActivity.java

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

    View v = getCurrentFocus();/*from   w ww  .j a va  2s.  c  o  m*/
    boolean ret = super.dispatchTouchEvent(event);

    if (v instanceof EditText) {
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];

        Log.d("Activity",
                "Touch event " + event.getRawX() + "," + event.getRawY() + " " + x + "," + y + " rect "
                        + w.getLeft() + "," + w.getTop() + "," + w.getRight() + "," + w.getBottom() + " coords "
                        + scrcoords[0] + "," + scrcoords[1]);
        if (event.getAction() == MotionEvent.ACTION_UP
                && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
        }
    }
    return ret;
}

From source file:com.apptentive.android.sdk.util.image.PreviewImageView.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    gestureDetector.onTouchEvent(event);
    scaleGestureDetector.onTouchEvent(event);

    float x = 0, y = 0;
    // Get multiple touch points
    final int pointerCount = event.getPointerCount();
    // Calculate average x and y
    for (int i = 0; i < pointerCount; i++) {
        x += event.getX(i);/*from  w w w.  j  a  v a2s  .  co  m*/
        y += event.getY(i);
    }
    x = x / pointerCount;
    y = y / pointerCount;

    /**
     * Reset lastX and lastY
     */
    if (pointerCount != lastPointerCount) {
        isCanDrag = false;
        lastX = x;
        lastY = y;
    }

    lastPointerCount = pointerCount;

    switch (event.getAction()) {
    case MotionEvent.ACTION_MOVE:
        float dx = x - lastX;
        float dy = y - lastY;

        if (!isCanDrag) {
            isCanDrag = isCanDrag(dx, dy);
        }
        if (isCanDrag) {
            RectF rectF = getMatrixRectF();
            if (getDrawable() != null) {
                isCheckLeftAndRight = isCheckTopAndBottom = true;
                // No left/right translation if image width is less than screen width
                if (rectF.width() < getWidth()) {
                    dx = 0;
                    isCheckLeftAndRight = false;
                }
                // No Up/Down translation if image height is less than screen height
                if (rectF.height() < getHeight()) {
                    dy = 0;
                    isCheckTopAndBottom = false;
                }
                scaleMatrix.postTranslate(dx, dy);
                checkMatrixBounds();
                setImageMatrix(scaleMatrix);
            }
        }
        lastX = x;
        lastY = y;
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        lastPointerCount = 0;
        break;
    }
    return true;
}

From source file:com.android.BluetoothManager.UI.viewpager.TitlePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//ww w.  j  a va2s.  c o m
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            if (!mViewPager.isFakeDragging()) {
                mViewPager.beginFakeDrag();
            }

            mLastMotionX = x;

            mViewPager.fakeDragBy(deltaX);
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                    return true;
                }
            } else {
                //Middle third
                if (mCenterItemClickListener != null) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, index);
        mLastMotionX = x;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.easemob.chatuidemo.activity.GroupDetailsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ??groupid/*from w w  w  . j a v  a2  s  .  c  o m*/
    groupId = getIntent().getStringExtra("groupId");
    group = EMGroupManager.getInstance().getGroup(groupId);

    // we are not supposed to show the group if we don't find the group
    if (group == null) {
        finish();
        return;
    }

    setContentView(R.layout.activity_group_details);
    instance = this;
    st = getResources().getString(R.string.people);
    clearAllHistory = (RelativeLayout) findViewById(R.id.clear_all_history);
    userGridview = (ExpandGridView) findViewById(R.id.gridview);
    loadingPB = (ProgressBar) findViewById(R.id.progressBar);
    exitBtn = (Button) findViewById(R.id.btn_exit_grp);
    deleteBtn = (Button) findViewById(R.id.btn_exitdel_grp);
    blacklistLayout = (RelativeLayout) findViewById(R.id.rl_blacklist);
    changeGroupNameLayout = (RelativeLayout) findViewById(R.id.rl_change_group_name);
    idLayout = (RelativeLayout) findViewById(R.id.rl_group_id);
    idLayout.setVisibility(View.VISIBLE);
    idText = (TextView) findViewById(R.id.tv_group_id_value);

    rl_switch_block_groupmsg = (RelativeLayout) findViewById(R.id.rl_switch_block_groupmsg);

    iv_switch_block_groupmsg = (ImageView) findViewById(R.id.iv_switch_block_groupmsg);
    iv_switch_unblock_groupmsg = (ImageView) findViewById(R.id.iv_switch_unblock_groupmsg);

    rl_switch_block_groupmsg.setOnClickListener(this);

    Drawable referenceDrawable = getResources().getDrawable(R.drawable.smiley_add_btn);
    referenceWidth = referenceDrawable.getIntrinsicWidth();
    referenceHeight = referenceDrawable.getIntrinsicHeight();

    idText.setText(groupId);
    if (group.getOwner() == null || "".equals(group.getOwner())
            || !group.getOwner().equals(EMChatManager.getInstance().getCurrentUser())) {
        exitBtn.setVisibility(View.GONE);
        deleteBtn.setVisibility(View.GONE);
        blacklistLayout.setVisibility(View.GONE);
        changeGroupNameLayout.setVisibility(View.GONE);
    }
    // 
    if (EMChatManager.getInstance().getCurrentUser().equals(group.getOwner())) {
        exitBtn.setVisibility(View.GONE);
        deleteBtn.setVisibility(View.VISIBLE);
    }

    ((TextView) findViewById(R.id.group_name))
            .setText(group.getGroupName() + "(" + group.getAffiliationsCount() + st);

    List<String> members = new ArrayList<String>();
    members.addAll(group.getMembers());

    adapter = new GridAdapter(this, R.layout.grid, members);
    userGridview.setAdapter(adapter);

    // ???group
    updateGroup();

    // OnTouchListener
    userGridview.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (adapter.isInDeleteMode) {
                    adapter.isInDeleteMode = false;
                    adapter.notifyDataSetChanged();
                    return true;
                }
                break;
            default:
                break;
            }
            return false;
        }
    });

    clearAllHistory.setOnClickListener(this);
    blacklistLayout.setOnClickListener(this);
    changeGroupNameLayout.setOnClickListener(this);

}

From source file:com.acceleratedio.pac_n_zoom.AnimActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_anm);
    orgnlImageView = (ImageView) findViewById(R.id.imageView);
    orgnlImageView.setMaxHeight(800);//from   w w w . j av a 2 s. c  o  m
    orgnlImageView.setMaxWidth(600);
    crt_ctx = this;

    BitmapFactory.Options bmp_opt = new BitmapFactory.Options();
    bmp_opt.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;

    // - Now we need to set the GUI ImageView data with data read from the picked file.
    DcodRszdBmpFil dcodRszdBmpFil = new DcodRszdBmpFil();
    Bitmap bmp = dcodRszdBmpFil.DcodRszdBmpFil(SelectImageActivity.orgFil, bmp_opt);

    // Now we need to set the GUI ImageView data with the orginal file selection.
    orgnlImageView.setImageBitmap(bmp);
    orgnl_iv_wdth = bmp.getWidth();
    orgnl_iv_hght = bmp.getHeight();
    final RelativeLayout rel_anm_lo = (RelativeLayout) findViewById(R.id.activity_anm_lo);
    scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());

    orgnlImageView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getPointerCount() > 1 || flgInScale) {

                scaleGestureDetector.onTouchEvent(event);
                return true;
            }

            int end_hrz;
            int end_vrt;
            final int pointerIndex;

            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                pointerIndex = MotionEventCompat.getActionIndex(event);
                bgn_hrz = (int) MotionEventCompat.getX(event, pointerIndex);
                bgn_vrt = (int) MotionEventCompat.getY(event, pointerIndex);

                String log_str = "Beginning coordinates: Horz = " + String.valueOf(bgn_hrz) + "; Vert = "
                        + String.valueOf(bgn_vrt);

                Log.d("OnTouchListener", log_str);
                orlp = (RelativeLayout.LayoutParams) orgnlImageView.getLayoutParams();
                bgn_top = (int) orlp.topMargin;
                bgn_lft = (int) orlp.leftMargin;

                // To prevent an initial jump of the magnifier, aposX and aPosY must
                // have the values from the magnifier frame
                if (aPosX == 0)
                    aPosX = orgnlImageView.getX();
                if (aPosY == 0)
                    aPosY = orgnlImageView.getY();
                break;

            case MotionEvent.ACTION_MOVE:

                pointerIndex = MotionEventCompat.getActionIndex(event);
                float crt_hrz = MotionEventCompat.getX(event, pointerIndex);
                float crt_vrt = MotionEventCompat.getY(event, pointerIndex);
                final float dx = crt_hrz - bgn_hrz;
                final float dy = crt_vrt - bgn_vrt;
                aPosX += dx;
                aPosY += dy;
                orgnlImageView.setX(aPosX);
                orgnlImageView.setY(aPosY);

                log_str = "Current Position: Horz = " + String.valueOf(crt_hrz) + "; Vert = "
                        + String.valueOf(crt_vrt);

                Log.d("OnTouchListener", log_str);

                break;

            case MotionEvent.ACTION_UP:

                pointerIndex = MotionEventCompat.getActionIndex(event);
                end_hrz = (int) MotionEventCompat.getX(event, pointerIndex);
                end_vrt = (int) MotionEventCompat.getY(event, pointerIndex);
            }

            rel_anm_lo.invalidate();
            return true;
        }
    });

    sav_anm_btn = (Button) findViewById(R.id.sav_btn);

    sav_anm_btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View vw) {

            onClickFlg = 1;
            RelativeLayout rel_anm_lo = (RelativeLayout) findViewById(R.id.activity_anm_lo);
            rel_anm_lo.removeView(vw);
            Bitmap tnBmp = getWrtBmp("thumbnail", rel_anm_lo, 40);
            tnBmp.recycle();
            int vw_nmbr = anmViews.size();

            for (int vw_mbr = 1; vw_mbr < vw_nmbr; vw_mbr += 1) {

                anim_view = anmViews.get(vw_mbr);

                if (anim_view != null) {

                    Animation crt_anm = anim_view.getAnimation();

                    if (crt_anm != null)
                        crt_anm.cancel();

                    anim_view.setAnimation(null);
                    rel_anm_lo.removeView(anim_view);

                    // Garbage collect the bitmap
                    Drawable drawable = anim_view.getDrawable();

                    if (drawable instanceof BitmapDrawable) {
                        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
                        Bitmap anim_bmp = bitmapDrawable.getBitmap();
                        anim_bmp.recycle();
                    }
                }
            }

            Bitmap orgnlImageBmp = getWrtBmp("bgimg", rel_anm_lo, 90);
            orgnlImageWdth = Integer.toString(orgnlImageBmp.getWidth());
            orgnlImageHght = Integer.toString(orgnlImageBmp.getHeight());
            anmViews.clear();
            unbindDrawables(rel_anm_lo);
            ((RelativeLayout) rel_anm_lo).removeAllViews();
            orgnlImageBmp.recycle();
            crt_ctx = null;
            orgnlImageView = null;

            Intent intent = new Intent(AnimActivity.this, com.acceleratedio.pac_n_zoom.SaveAnmActivity.class);

            startActivity(intent);
        }
    });

    progress = ProgressDialog.show(crt_ctx, "Loading the animation", "dialog message", true);
    GetRequest get_svg_img = new GetRequest();
    get_svg_img.execute("");
}

From source file:com.alibaba.akita.widget.TitlePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//from w  w  w .ja v a 2s . c  o  m
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            if (!mViewPager.isFakeDragging()) {
                mViewPager.beginFakeDrag();
            }

            mLastMotionX = x;
            try {
                mViewPager.fakeDragBy(deltaX);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                    return true;
                }
            } else {
                //Middle third
                if (mCenterItemClickListener != null) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging()) {
            try {
                mViewPager.endFakeDrag();
            } catch (NullPointerException npe) {
                npe.printStackTrace(); // fix a crash report.
            }
        }
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, index);
        mLastMotionX = x;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.borqs.qiupu.fragment.TitlePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }// w  w w .j a v a2  s .  c  o  m
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            //TODO
            //                    if (!mViewPager.isFakeDragging()) {
            //                        mViewPager.beginFakeDrag();
            //                    }

            mLastMotionX = x;

            //TODO
            //                    mViewPager.fakeDragBy(deltaX);
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                    return true;
                }
            } else {
                //Middle third
                if (mCenterItemClickListener != null) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        //TODO
        //                if (mViewPager.isFakeDragging()) mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, index);
        mLastMotionX = x;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.thousandthoughts.tutorials.SensorFusionActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//  w  w w  .j av  a  2s  . c o  m
    layout = (RelativeLayout) findViewById(R.id.mainlayout);
    IP1 = "";
    IP2 = "";
    angle1 = 0.0f;
    angle2 = 0.0f;
    gyroOrientation[0] = 0.0f;
    gyroOrientation[1] = 0.0f;
    gyroOrientation[2] = 0.0f;

    // initialise gyroMatrix with identity matrix
    gyroMatrix[0] = 1.0f;
    gyroMatrix[1] = 0.0f;
    gyroMatrix[2] = 0.0f;
    gyroMatrix[3] = 0.0f;
    gyroMatrix[4] = 1.0f;
    gyroMatrix[5] = 0.0f;
    gyroMatrix[6] = 0.0f;
    gyroMatrix[7] = 0.0f;
    gyroMatrix[8] = 1.0f;

    // get sensorManager and initialise sensor listeners
    mSensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
    initListeners();

    // wait for one second until gyroscope and magnetometer/accelerometer
    // data is initialised then scedule the complementary filter task
    fuseTimer.scheduleAtFixedRate(new calculateFusedOrientationTask(), 1000, TIME_CONSTANT);

    // GUI stuff
    try {
        client1 = new MyCoapClient(this.IP1);
        client2 = new MyCoapClient(this.IP2);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    json = new JSONObject();
    mHandler = new Handler();
    radioSelection = 0;
    d.setRoundingMode(RoundingMode.HALF_UP);
    d.setMaximumFractionDigits(3);
    d.setMinimumFractionDigits(3);

    /// Application layout here only

    RelativeLayout.LayoutParams left = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams right = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams up = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams down = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams button1 = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams button2 = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams text1 = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams text2 = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    ImageView img_left = new ImageView(this);
    ImageView img_right = new ImageView(this);
    ImageView img_up = new ImageView(this);
    ImageView img_down = new ImageView(this);
    ImageView img_button1 = new ImageView(this);
    ImageView img_button2 = new ImageView(this);
    final EditText edittext1 = new EditText(this);
    final EditText edittext2 = new EditText(this);
    img_left.setImageResource(R.drawable.left_button);
    img_right.setImageResource(R.drawable.right_button);
    img_up.setImageResource(R.drawable.up);
    img_down.setImageResource(R.drawable.down);
    img_button1.setImageResource(R.drawable.button);
    img_button2.setImageResource(R.drawable.button);

    left.setMargins(0, 66, 0, 0);
    right.setMargins(360, 66, 0, 0);
    up.setMargins(240, 90, 0, 0);
    down.setMargins(240, 240, 0, 0);
    button1.setMargins(440, 800, 0, 0);
    button2.setMargins(440, 900, 0, 0);
    text1.setMargins(5, 800, 0, 0);
    text2.setMargins(5, 900, 0, 0);

    layout.addView(img_left, left);
    layout.addView(img_right, right);
    layout.addView(img_up, up);
    layout.addView(img_down, down);
    layout.addView(img_button1, button1);
    layout.addView(edittext1, text1);
    layout.addView(img_button2, button2);
    layout.addView(edittext2, text2);

    img_left.getLayoutParams().height = 560;
    img_left.getLayoutParams().width = 280;
    img_right.getLayoutParams().height = 560;
    img_right.getLayoutParams().width = 280;
    img_up.getLayoutParams().height = 150;
    img_up.getLayoutParams().width = 150;
    img_down.getLayoutParams().height = 150;
    img_down.getLayoutParams().width = 150;
    img_button1.getLayoutParams().height = 100;
    img_button1.getLayoutParams().width = 200;
    edittext1.getLayoutParams().width = 400;
    edittext1.setText("84.248.76.84");
    edittext2.setText("84.248.76.84");
    img_button2.getLayoutParams().height = 100;
    img_button2.getLayoutParams().width = 200;
    edittext2.getLayoutParams().width = 400;

    /////////// Define and Remember Position for EACH PHYSICAL OBJECTS (LAPTOPS) /////////////////////
    img_button1.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            // TODO Auto-generated method stub
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // POSITION OF LAPTOP 1 IS REMEMBERED
                angle1 = fusedOrientation[0];
                IP1 = edittext1.getText().toString();
                try {
                    // CREATE CLIENT CONNECT TO FIRST LAPTOP
                    client1 = new MyCoapClient(IP1);
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                //view.setImageResource(R.drawable.left_button);
                break;
            }
            return true;
        }

    });

    img_button2.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            // TODO Auto-generated method stub
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // POSITION OF LAPTOP 2 IS REMEMBERED
                angle2 = fusedOrientation[0];
                IP2 = edittext2.getText().toString();
                try {
                    // CREATE CLIENT CONNECT TO SECOND LAPTOP
                    client2 = new MyCoapClient(IP2);
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                break;
            }
            return true;
        }

    });

    img_left.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                view.setImageResource(R.drawable.left_button_press);

                ///////////////////// PERFORM CLICK ACTION BASED ON POSITION OF 2 PHYSICAL OBJECTS (LAPTOPs) HERE/////////////////////////

                // PHONE's ANGLE WITHIN FIRST LAPTOP//
                if (angle1 != 0.0f) {
                    if (angle1 * 180 / Math.PI - 40.0 < fusedOrientation[0] * 180 / Math.PI
                            && fusedOrientation[0] * 180 / Math.PI < angle1 * 180 / Math.PI + 40.0) {
                        client1.runClient("left pressed");
                    }
                }
                //PHONE's ANGLE WITHIN SECOND LAPTOP//
                if (angle2 != 0.0f) {
                    if (angle2 * 180 / Math.PI - 40.0 < fusedOrientation[0] * 180 / Math.PI
                            && fusedOrientation[0] * 180 / Math.PI < angle2 * 180 / Math.PI + 40.0) {
                        client2.runClient("left pressed");
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                view.setImageResource(R.drawable.left_button);
                // PHONE's ANGLE WITHIN FIRST LAPTOP//
                if (angle1 != 0.0f) {
                    if (angle1 * 180 / Math.PI - 40.0f < fusedOrientation[0] * 180 / Math.PI
                            && fusedOrientation[0] * 180 / Math.PI < angle1 * 180 / Math.PI + 40.0f) {
                        client1.runClient("left released");
                    }
                }
                //PHONE's ANGLE WITHIN SECOND LAPTOP//
                if (angle2 != 0.0f) {
                    if (angle2 * 180 / Math.PI - 40.0f < fusedOrientation[0] * 180 / Math.PI
                            && fusedOrientation[0] * 180 / Math.PI < angle2 * 180 / Math.PI + 40.0f) {
                        client2.runClient("left released");
                    }
                }
                break;
            }
            return true;
        }
    });

    img_right.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                view.setImageResource(R.drawable.right_button_press);
                // PHONE's ANGLE WITHIN FIRST LAPTOP//
                if (angle1 != 0.0f) {
                    if (angle1 * 180 / Math.PI - 40.0f < fusedOrientation[0] * 180 / Math.PI
                            && fusedOrientation[0] * 180 / Math.PI < angle1 * 180 / Math.PI + 40.0f) {
                        client1.runClient("right pressed");
                    }
                }
                //PHONE's ANGLE WITHIN SECOND LAPTOP//
                if (angle2 != 0.0f) {
                    if (angle2 * 180 / Math.PI - 40.0f < fusedOrientation[0] * 180 / Math.PI
                            && fusedOrientation[0] * 180 / Math.PI < angle2 * 180 / Math.PI + 40.0f) {
                        client2.runClient("right pressed");
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                view.setImageResource(R.drawable.right_button);
                break;
            }
            return true;
        }
    });

    img_up.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                view.setImageResource(R.drawable.up_press);
                // PHONE's ANGLE WITHIN FIRST LAPTOP//
                if (angle1 != 0.0f) {
                    if (angle1 * 180 / Math.PI - 40.0f < fusedOrientation[0] * 180 / Math.PI
                            && fusedOrientation[0] * 180 / Math.PI < angle1 * 180 / Math.PI + 40.0f) {
                        client1.runClient("up pressed");
                    }
                }
                //PHONE's ANGLE WITHIN SECOND LAPTOP//
                if (angle2 != 0.0f) {
                    if (angle2 * 180 / Math.PI - 40.0f < fusedOrientation[0] * 180 / Math.PI
                            && fusedOrientation[0] * 180 / Math.PI < angle2 * 180 / Math.PI + 40.0f) {
                        client2.runClient("up pressed");
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                view.setImageResource(R.drawable.up);
                break;
            }
            return true;
        }
    });

    img_down.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                view.setImageResource(R.drawable.down_press);
                // PHONE's ANGLE WITHIN FIRST LAPTOP//
                if (angle1 != 0.0f) {
                    if (angle1 * 180 / Math.PI - 40.0f < fusedOrientation[0] * 180 / Math.PI
                            && fusedOrientation[0] * 180 / Math.PI < angle1 * 180 / Math.PI + 40.0f) {
                        client1.runClient("down pressed");
                    }
                }
                //PHONE's ANGLE WITHIN SECOND LAPTOP//
                if (angle2 != 0.0f) {
                    if (angle2 * 180 / Math.PI - 40.0f < fusedOrientation[0] * 180 / Math.PI
                            && fusedOrientation[0] * 180 / Math.PI < angle2 * 180 / Math.PI + 40.0f) {
                        client2.runClient("down pressed");
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                view.setImageResource(R.drawable.down);
                break;
            }
            return true;
        }
    });
}