Handle TouchView / TextView drag events differently

I am developing an application where I need to both drag a text view and display a dialog when I click on it.

I have added my code below and when I only use ACTION_MOVE

I can drag and drop the text. However, when I put TextDialog.setVisibility(View.VISIBLE);

to make my dialog visible, I cannot drag the text. Both events do not work at the same time.

How can I handle both events?

tvText=  (TextView)findViewById(R.id.text);
    TextDialog=(LinearLayout)findViewById(R.id.Textdialog);
    tvText.setOnTouchListener(this);
    tvText.setOnClickListener(this);
}


@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    layoutParams = (LayoutParams) tvText.getLayoutParams();
    switch(event.getAction())
    {
    case MotionEvent.ACTION_DOWN:  
                        TextDialog.setVisibility(View.VISIBLE);
                                    break;
    case MotionEvent.ACTION_MOVE:
                                    int x = (int)event.getRawX();
                                    int y= (int)event.getRawY();



                                    layoutParams.leftMargin = x - 150;
                                    layoutParams.topMargin = y - 210;


                                    tvText.setLayoutParams(layoutParams);
                                    break;

          default:
                                    break;
    }
          return true;



}

      

+3


source to share


2 answers


Implement OnClickListener. Check this



myView.setOnTouchListener(myListener);
myView.setOnClickListener(myListener);

/* MyListener class */
class MyListener implements View.OnTouchListerner, View.OnClickListener {
public void onTouch(View v, MotionEvent e) {
    if (e.ACTION_MOVE) {
        Log.d("ACTION MOVE",""); // now it is called
    } else if (e.ACTION_DOWN) {
        Log.d("ACTION_DOWN"); // called
    }
}

    public void onClick(View v) {
    }
}

      

0


source


drag and drop all widgets or layouts and place them in onTouch ()



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

        final int X = (int) event.getRawX() - minusDisplayVal;
        final int Y = (int) event.getRawY() - minusDisplayVal;

        LinearLayout.LayoutParams layoutParamsSub = (LinearLayout.LayoutParams) view1
                .getLayoutParams();
        int _xDelta = 0;
        int _yDelta = 0;

        switch (event.getActionMasked() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            _xDelta = X - layoutParamsSub.leftMargin;
            _yDelta = Y - layoutParamsSub.topMargin;
            break;

        case MotionEvent.ACTION_UP:
            _xDelta = X - layoutParamsSub.leftMargin;
            _yDelta = Y - layoutParamsSub.topMargin;
            break;

        case MotionEvent.ACTION_POINTER_DOWN:
            _xDelta = X - layoutParamsSub.leftMargin;
            _yDelta = Y - layoutParamsSub.topMargin;
            break;

        case MotionEvent.ACTION_POINTER_UP:
            _xDelta = X - layoutParamsSub.leftMargin;
            _yDelta = Y - layoutParamsSub.topMargin;
            break;

        case MotionEvent.ACTION_MOVE:

            layoutParamsSub.leftMargin = X - _xDelta;
            layoutParamsSub.topMargin = Y - _yDelta;

            if (layoutParamsSub.leftMargin < 0)// //for left
                layoutParamsSub.leftMargin = 0;

            if (layoutParamsSub.topMargin < 0)// ///for top
                layoutParamsSub.topMargin = 0;

            dataTv.setLayoutParams(layoutParamsSub);
            break;
        }
        backgroudColor.invalidate();
        return true;
    }

      

0


source







All Articles