Reputation: 101
I have a XML layout, on which I'm applying OnTouchListener, which task is to detect finger swipe direction; anyway, it's working only when swipe is performed just on layout, but not on it's items (like ImageViews, Buttons etc.). How do I rewrite it to make swipe working on whole layout, but avoid creating dozens of Listeners for all items on it?
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_layout"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
>
</RelativeLayout>
Code:
View relative = findViewById(R.id.main_layout);
relative.setOnTouchListener(...)
Upvotes: 1
Views: 787
Reputation: 116
You can use following code to make a perfect android gesture:
Type following code at Main_activity.java:
package com.androidtutorialpoint.myapplication; public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener { private TextView output_text; private GestureDetectorCompat DetectMe; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); output_text = (TextView) findViewById(R.id.outputText); // Taking reference of text to be displayed on screen DetectMe = new GestureDetectorCompat(this,this); DetectMe.setOnDoubleTapListener(this); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } // Following functions are overrided to show text when a particular method called. @Override public boolean onSingleTapConfirmed(MotionEvent e) { output_text.setText("onSingleTapConfirmed"); return true; } @Override public boolean onDoubleTap(MotionEvent e) { output_text.setText("onDoubleTap"); return true; } @Override public boolean onDoubleTapEvent(MotionEvent e) { output_text.setText("onDoubleTapEvent"); return true; } @Override public boolean onDown(MotionEvent e) { output_text.setText("onDown"); return true; } @Override public void onShowPress(MotionEvent e) { output_text.setText("onShowPress"); } @Override public boolean onSingleTapUp(MotionEvent e) { output_text.setText("onSingleTapUp"); return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { output_text.setText("onScroll"); return true; } @Override public void onLongPress(MotionEvent e) { output_text.setText("onLongPress"); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { output_text.setText("onFling"); return true; } @Override public boolean onTouchEvent(MotionEvent event){ this.DetectMe.onTouchEvent(event); return super.onTouchEvent(event); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Upvotes: 0
Reputation: 441
Because you are setting OnclickListener for only one view which is parent. so that's why there is only one click response. You can try this.
Button button1 = (Button) findviewbyid(R.id.buttonid);
button.setOnTouchListener(...)
Similarly images or other views. Edit - DO this for every view that you want to listn swipe for.
ImageView im = (ImgaeView) findviewbyid(R.id.imageid)
im.setOnTouchListener(...)
Similarly with Every View for Listning Swipe.
EDIT- So you don't wanna make more ONTouchListener. Make another class which implements OnTouchListener
public class Mytouchlistener implements OnTouchlistener{
@Override
public boolean onTouch(View v, MotionEvent event) {
HERE PUT YOUR CODE.
}
Then
Youanyview.OnTouchlistener(MyTouchlistener.class);
Got the idea???
Upvotes: 1
Reputation: 332
you can implement View.OnTouchListener
as shown below
public class SomeClass implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()){
case R.id.view1:
//do something
break;
case R.id.view2:
//do something here too
break;
default:
break;
}
return false;
}
}
be aware that you must use setOnTouchListener
.
good luck .
Upvotes: 1