0

In my project i need to use the onTouchListener in order for me to know when the user touch\don't touch specific buttons and send data to my server in both cases. the code:

 private View.OnTouchListener handleTouch = new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {

    switch (v.getId()) {
        case R.id.Abtn:
            onControl("BA",v, event);
            break;
        case R.id.Bbtn:
            onControl("BB", v, event);
            break;
        case R.id.Xbtn:
            onControl("BX",v, event);
            break;
        case R.id.Ybtn:
            onControl("BY",v, event);
            break;
        case R.id.Rbtn:
            onControl("BR",v, event);
            break;
        case R.id.Lbtn:
            onControl("BL",v, event);
            break;
        case R.id.UpBtn:
            onControl("BU",v, event);
            break;
        case R.id.Dbtn:
            onControl("BD",v, event);
            break;
        case R.id.RTbtn:
            onControl("RT",v, event);
            break;
        case R.id.LTbtn:
            onControl("LT",v, event);
            break;
        case R.id.Start_btn:
            onControl("ST",v, event);
            break;
        case R.id.Back_btn:
            onControl("CK",v, event);
            break;
        case R.id.RB_btn:
            onControl("RB",v, event);
            break;
        case R.id.LB_btn:
            onControl("LB",v, event);
            break;


    }
    return true;
}
};

i tried to add action_cancle - the problem didnt solve yet. basicly what happend is that i only recieve the action down event \ data that i send and the if of action up never recognize. i checked all over the internet and the common answer was to return true in every case of onTouch function which makes sense but this is exacly what i do and still cant make it to work.

on control:

 private void onControl(String st,View v, MotionEvent event){
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        new SendMessage().execute("S"+ st);
    }
    if(event.getAction() == MotionEvent.ACTION_UP){
        new SendMessage().execute("P "+ st);
    }
}

layout:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
    android:id="@+id/Bbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="20dp"
    android:layout_marginTop="84dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/button_b" />

<ImageButton
    android:id="@+id/Xbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="84dp"
    android:layout_marginStart="120dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/button_x" />

<ImageButton
    android:id="@+id/Abtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="134dp"
    android:layout_marginStart="70dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/button_a" />

<ImageButton
    android:id="@+id/Ybtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="34dp"
    android:layout_marginStart="70dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/button_y" />
<ImageButton
    android:id="@+id/RTbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="210dp"
    android:layout_marginTop="30dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/rt_button" />
<ImageButton
    android:id="@+id/LTbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="500dp"
    android:layout_marginTop="30dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/lt_button" />


<ImageButton
    android:id="@+id/UpBtn"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="500dp"
    android:layout_marginTop="230dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:rotation="-90"
    android:src="@drawable/sides_b" />

<ImageButton
    android:id="@+id/Rbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="465dp"
    android:layout_marginTop="265dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/sides_b" />

<ImageButton
    android:id="@+id/Lbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="535dp"
    android:layout_marginTop="265dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:rotation="180"
    android:src="@drawable/sides_b" />
<ImageButton
    android:id="@+id/Dbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="500dp"
    android:layout_marginTop="300dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:rotation="90"
    android:src="@drawable/sides_b" />

<ImageButton
    android:id="@+id/Start_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="300dp"
    android:layout_marginTop="50dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/start_btn" />
<ImageButton
    android:id="@+id/Back_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="420dp"
    android:layout_marginTop="50dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:rotation="180"
    android:src="@drawable/start_btn" />
<ImageButton
    android:id="@+id/LB_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="470dp"
    android:layout_marginTop="130dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/lb_btn" />
<ImageButton
    android:id="@+id/RB_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="210dp"
    android:layout_marginTop="130dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/rb_btn" />

setting the listeners:

 private void initView(){
    ImageButton A =  findViewById(R.id.Abtn);
    ImageButton B = findViewById(R.id.Bbtn);
    ImageButton X =  findViewById(R.id.Xbtn);
    ImageButton Y =  findViewById(R.id.Ybtn);
    ImageButton Rside =  findViewById(R.id.Rbtn);
    ImageButton Lside =  findViewById(R.id.Lbtn);
    ImageButton Up =  findViewById(R.id.UpBtn);
    ImageButton Down =  findViewById(R.id.Dbtn);
    ImageButton Rt =  findViewById(R.id.RTbtn);
    ImageButton Lt =  findViewById(R.id.LTbtn);
    ImageButton Start = findViewById(R.id.Start_btn);
    ImageButton Back = findViewById(R.id.Back_btn);
    ImageButton Rb = findViewById(R.id.RB_btn);
    ImageButton Lb = findViewById(R.id.LB_btn);

    A.setOnTouchListener(handleTouch);
    B.setOnTouchListener(handleTouch);
    X.setOnTouchListener(handleTouch);
    Y.setOnTouchListener(handleTouch);
    Rside.setOnTouchListener(handleTouch);
    Lside.setOnTouchListener(handleTouch);
    Up.setOnTouchListener(handleTouch);
    Down.setOnTouchListener(handleTouch);
    Rt.setOnTouchListener(handleTouch);
    Lt.setOnTouchListener(handleTouch);
    Start.setOnTouchListener(handleTouch);
    Back.setOnTouchListener(handleTouch);
    Rb.setOnTouchListener(handleTouch);
    Lb.setOnTouchListener(handleTouch);

}
Ido
  • 55
  • 7
  • Have you seen https://stackoverflow.com/a/12588509/3974530 or https://stackoverflow.com/questions/15799839/motionevent-action-up-not-called – InsaneCat Apr 04 '18 at 13:17
  • i have seen those posts but check out that my problem isnt with the return true.. – Ido Apr 04 '18 at 13:52

1 Answers1

0

If you're using the view inside a view group, then maybe the ViewGroup is taking control over your touch intercepts,

try: requestDisallowInterceptTouchEvent(true);

inside the place where you are listening for your ACTION_DOWN event.

Edit your onControl() method to:

private void onControl(String st,View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_DOWN){
   requestDisallowInterceptTouchEvent(true);
   new SendMessage().execute("S"+ st);
}
else if(event.getAction() == MotionEvent.ACTION_UP){
   requestDisallowInterceptTouchEvent(false);
   new SendMessage().execute("P "+ st);
}} 

Im not sure of this, but is worth a try.

MadScientist
  • 2,134
  • 14
  • 27