How to detect when button pressed and released on android

Use OnTouchListener instead of OnClickListener: // this goes somewhere in your class: long lastDown; long lastDuration; … // this goes wherever you setup your button listener: button.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { lastDown = System.currentTimeMillis(); } else if (event.getAction() == MotionEvent.ACTION_UP) { lastDuration = System.currentTimeMillis() – … Read more

Android. How do I keep a button displayed as PRESSED until the action created by that button is finished?

I used a function like void setHighlighted(boolean highlight) { button.setBackgroundResource( highlight ? R.drawable.bbg_pressed : R.drawable.button_background); } where button_background is a selector defined in button_backgroung.xml: <?xml version=”1.0″ encoding=”utf-8″?> <selector xmlns:android=”http://schemas.android.com/apk/res/android” > <item android:state_pressed=”true” android:drawable=”@drawable/bbg_pressed”></item> <item android:state_focused=”true” android:drawable=”@drawable/bbg_selected”></item> <item android:drawable=”@drawable/bbg_normal”></item> </selector> That is, this code does not interfere with the pressed state used by the Android framework; … Read more

How can I keep a button as pressed after clicking on it? [duplicate]

I had this issue with a button with a custom background, and ended up using the selected state for this. That state is available for all views. To use this you have to define a custom button background as a state list: <selector xmlns:android=”http://schemas.android.com/apk/res/android”> <item android:state_selected=”false” android:state_focused=”false” android:state_pressed=”false”><bitmap … /></item> <item android:state_selected=”true”><bitmap … /></item> <item … Read more