0

I have a row of buttons in my app. What i would like to achieve is : when i drag my finger over the button, a method would be called.

My code looks like this so far :

Layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.a4web.example.MainActivity">

</LinearLayout>

Java :

public class MainActivity extends AppCompatActivity {
LinearLayout rootView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rootView = (LinearLayout) findViewById(R.id.activity_main);

    for(int i=0; i<5; i++){
        Button button = new Button(this);
        button.setText(i);
        button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        doSomething();
                        break;
                }

                return false;
            }
        });
        rootView.addView(button);
    }
}

My method doSomething only gets called when i press on the button. How could i achieve that it is called when i drag over a button. What kind of event should i be listening to ?

Aleš
  • 13
  • 6
  • You can override OnTouchListener to get your SwipeListener. Use this example http://stackoverflow.com/a/12938787/3286614 – Rachit Sep 21 '16 at 08:39

1 Answers1

0

To do that you have to use the onTouchListener and not the onClickListener that triggers out only when you press the button and release it.

Even if the onTouchListener works for the three events MotionEvent.ACTION_DOWN (when you press), MotionEvent.ACTION_MOVE (when you drag the finger), MotionEvent.ACTION_UP (when you release the button), you still have to press the button and then drag over it. That means that you can't reveal the drag event on your button if you click outside the button first.

If you want to click everywhere and then check the drag event on your button, this is still a bad way to do it.

I suggest you to make all the view cliccable and to implement an onTouchListener on the whole view and chech when your finger is on the specific portion of your view.

To do so, it's important to make the buttons not clickable, otherwise they will avoid the whole view to be cliccable at all!

If you want to learn how to implement a cliccable view you can have a look to this repository:

https://github.com/alessandroargentieri/JoyStick

Alessandro Argentieri
  • 2,901
  • 3
  • 32
  • 62