I am looking for a way to call a function when a button is pressed and held for one second. When the button is released another function should be called.
I was thinking about an onLongClickedListener but this won't work well for me since the text that is going to be displayed would stay too long or short. I am thinking a TouchListener could help me because the Action_Up event would give me the option to let the text dissapear when the button isn't pressed anymore. The Action_down event gives me when the button is pressed and I thought I could start a timer when the button is pressed, wait a second, check again if the button still is pressed and then call the function (show the text).
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
// the button. I set bFertig.setOnTouchListener(this); in onCreate
case R.id.bFertig:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// everything works up to here
waitTimerNotif = new CountDownTimer(1000, 500) {
@Override
public void onTick(long arg0) {}
@Override
public void onFinish() {
// TODO Auto-generated method stub
// here im checking if the button still is pressed
if (bFertig.isPressed()) {
// It never goes into here
ShowNotifBox("Fertig", "fertig", false, false,false);
}
}
}.start();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
DissapearNotifBox();
Log.d("Debug", "Button released");
}
break;
}
return true;
}
For the Button in xml:
<Button
android:id="@+id/bFertig"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/fertigbutton"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="18.5dp"
android:clickable="true"/> <!--Googleing suggested I need this for isPressed() to work but it didnt help
Any ideas what I did wrong or why this isnt working? Thanks!
Why not use the setOnLongClickListener
function? This should solve your issue. An example can be find here: setOnLongClickListener
You're returning true, which tells the system that you are handling the touch events, which means the button is not, which means it's probably not updating its pressed state. Try returning false.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.