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 ?