I have a slidingdrawer in my activity, i open sliding drawer on swipe gesture from bottom to top what i want is to close that opened drawer after some second or delay let say after 5 seconds. How can i do that?
private class GestureDetector extends SimpleOnGestureListener {
SlidingDrawer drawer;
Timer timer;
public GestureDetector(SlidingDrawer drawer) {
this.drawer = drawer;
timer = new Timer();
}
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
closeSlider();
}
};
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1.getY() > e2.getY()) {
this.drawer.animateOpen();
timer.scheduleAtFixedRate( timerTask , 0, 3000);
} else {
//this.drawer.animateClose();
}
}
public void closeSlider() {
this.drawer.animateClose();
}
}
//i call it on activity onCreate()
SlidingDrawer slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
new GestureDetector(slidingDrawer);
Thanking you
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO call closeSlider method here.
}
}, 5000);
Related
I have a button and when I hold the button for 2 seconds, I want to show an AlertDialog.
This is what I have tried:
btn.setOnTouchListener(new OnSwipeTouchListener(getContext()) {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Helper.setTimeout(() -> {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Message");
builder.setMessage(text);
builder.show();
System.out.println("I am text");
}
}, 2000);
return super.onTouch(view, motionEvent);
}
}
My method setTimeout():
public static void setTimeout(Runnable runnable, int delay){
new Handler(Looper.getMainLooper()).postDelayed(runnable, delay);
}
My problem is that sometimes the AlertDialog shows up multiple times and I always get this warning:
W/InputEventReceiver: Attempted to finish an input event but the input
event receiver has already been disposed.
What am I doing wrong?
I don't know if there is a better solution. I have also tried It with
btn.setOnLongClickListener(v -> {
System.out.println("hold");
return true;
});
but that doesn't output anything.
You can try this
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Message");
builder.setMessage("hello");
builder.show();
}
}, 2000);
return false;
}
});
}
}
My splash screen diseapear after 2 seconds but I want it to diseapear also after a screen touch
Like if I press screen I moved next page without waiting until the end of the timer.
Is it possible??
public class SplashActivity extends Activity {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashfile);
handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
},2000);
}
}
Tes it is possible, you can add a touch listener to your layout that do the same as what will happen after two seconds , some thing like that
findViewById(R.id.myLayout).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return true;
}
});
EDIT :
or you can instead add the next code outside onCeate method if the layout don't receive the touch event:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return super.dispatchTouchEvent(ev);
}
I have a messaging app similar to whatsapp where there are messages videos and images in the chat activity. Now I want to implement a double tap like feature on this but the problem is I also have a onclicklistner attached to my image and video frame that opens up a fullscreenactivity showing the image in fullscreen or playing the video in fullscreen. So on double tap instead of liking the picture or video it simply opens the video or image in the fullscreen mode. Since in messages I don't have a onclick listner attached double tap like works fine. Any ideas on how I can make the double tap like work in case of the videos or images. I am fairly new to android development using Java. So any help is appreciated.
HI, I think the approach to take depends on the future of your app, using LongClick is kinda good but what if you want to achieve a zoom like onLongClick?
What I did below is that I created a subclass that extends GestureDetector.SimpleOnGestureListener and then in my GestureTest class, I an ontouchListener and return the gesture instance created before, finally I parse the listener to the button that has to perform the clicks, it can be any View object.
Finally, I used the TextView I created to display/monitor my actions.
GestureDetector like below:
public class GestureTest extends AppCompatActivity{
private Button btn;
private TextView actions;
private GestureDetector mDetector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture_test);
btn = findViewById(R.id.btn);
actions = findViewById(R.id.action);
mDetector = new GestureDetector(this, new SampleGestureListener());
View.OnTouchListener touchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return mDetector.onTouchEvent(event);
}
};
btn.setOnTouchListener(touchListener);
}
public class SampleGestureListener extends GestureDetector.SimpleOnGestureListener{
#Override
public boolean onDown(MotionEvent event) {
Log.e("EVENT","BUTTON PRESSED DOWN, NOT YET RELEASED WILL TRIGGER ONLONG-PRESS");
// don't return false here or else none of the other
// gestures will work
return true;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.e("EVENT", "SINGLE CLICK EVENT");
actions.setText("THIS CLICK IS FOR IMAGE OR VIDEO TO OPEN ?");
return true;
}
#Override
public void onLongPress(MotionEvent e) {
Log.e("EVENT", "LONG PRESSED");
actions.setText("THIS IS ZOOM LIKE NOT DOUBLE CLICK!!!");
}
#Override
public boolean onDoubleTap(MotionEvent e) {
Log.e("EVENT", "DOUBLE CLICKED HAPPENED");
actions.setText("THIS CLICK IS TO LIKE IMAGE OR VIDEO ?");
return true;
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.e("EVENT", "SCROLLING");
actions.setText("YOU\" Scrolling on many images...");
return true;
}
#Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.e("EVENT", "FLINGED");
actions.setText("THIS IS TINDER FLING LIKE ?");
return true;
}
}
}
AND BELOW IS MY activity_gesture_test.xml FOR TESTING:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="100dp"/>
<TextView
android:id="#+id/action"
android:textSize="20dp"
android:textColor="#color/black"
app:layout_constraintTop_toBottomOf="#+id/btn"
android:layout_width="match_parent"
android:layout_height="100dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
GoodLuck Coding.
try this for double click:
int i = 0;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
i++;
Handler handler = new Handler();
Runnable r = new Runnable() {
#Override
public void run() {
i = 0;
}
};
if (i == 1) {
//Single click
handler.postDelayed(r, 250);
} else if (i == 2) {
//Double click
i = 0;
ShowDailog();
}
}
});
and for click listener:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
It is generally accepted to use long tap instead of double click in android apps. Try this:
yourView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
//do Your stuff here
return true;
}
});
I'm using the AppCompat ActionBar (Theme.AppCompat.Light.DarkActionBar) for my Android app.
After starting the app, the ActionBar fades out after 6 seconds (onCreate method):
h.postDelayed(new Runnable() {
#Override
public void run() {
getSupportActionBar().hide();
}
}, 6000);
So, how can I fade in the ActionBar with a touch gesture from the top of the screen to the middle ... and fade out it again after 10 seconds?
I think the problem is, that the ActionBar has no OnTouchListener.
Try something like this.
In the class you want to use the gesture:
private GestureDetectorCompat gestureDetectorCompat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureDetectorCompat = new GestureDetectorCompat(this, new MyGestureListener());
//Stuff in onCreate()
}
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetectorCompat.onTouchEvent(event);
return super.onTouchEvent(event);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
//Swipe from the top to the bottom
if(event2.getY() > event1.getY()){
getSupportActionBar().show();
h.postDelayed(new Runnable() {
#Override
public void run() {
getSupportActionBar().hide();
}
}, 6000);
}
return true;
}
}
The Timer works fine. The problem is that the method _dw.newgame() just get executed(if two seconds are over) if i touch the display. Maybe it have sth to do with the "OnTouchListener" ?
public class DrawView extends View implements OnTouchListener{
Timer timer = new Timer();
public DrawView(Context context)
{
timer.schedule(new Task(this),0, 2000);
}
public boolean onTouch(View view, MotionEvent event)
{}
}
class Task extends TimerTask
{
private DrawView _dw;
public Task(DrawView dw){ this._dw = dw;}
public void run()
{ _dw.newgame(); }
}
Maybe you should start the timer in the onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(Bundle savedInstanceState);
timer = new Timer();
timer.schedule(new Task(this), 0, 2000);
}
second try with handler:
public class DrawView extends View implements OnTouchListener{
private Timer timer = new Timer();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(Bundle savedInstanceState);
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
handler.sendEmptyMessage();
}
}, 0, 2000);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
newgame();
}
};
}
I did not compile this but it should work like this.
I noticed another thing: I suppose newgame() should only be called once. timer.schedule(new Task(this), 0, 2000); does that every 2 secs.