The effect of clicking a button is doing this: the button.xml file in drawable :

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_today_t" android:state_pressed="true" /> <item android:drawable="@drawable/button_today_t" android:state_focused="true" /> <item android:drawable="@drawable/button_today_n" /> </selector> 

And in the button, respectively, android:background="@drawable/button"

Everything works as it should, but there is one thing, the button becomes visually pressed after ~ 0.2s after touching. In principle, this happens without a decorated button, even with a simple gray. Tell me please, is it possible to do something like this so that the button is visually pressed at the moment of touching? Thank.

UPD

  button_today.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ button_today.setPressed(true); return true; } return false; } }); 
  • try onClickListener to add a line mButton.setPressed (true) - Kirill Stoianov
  • @KirillStoianov, nothing has changed - Pollux
  • Well, you can still try instead of onClickListener hang OnTouchListener - Kirill Stoianov
  • @KirillStoianov, thanks! That helped. Updated the first post. It only works if the code is littered with each button like this) - Pollux
  • @Pollux and not tried onTouchEvent(MotionEvent event) ? in theory, this method should work faster - Senior Pomidor

1 answer 1

You need to create one instance of View.OnTouchListener and assign it to each button:

 View.OnTouchListener listener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ ((Button)findViewById(v.getId())).setPressed(true); return true; } return false; } } button_today.setOnTouchListener(listener); button_today2.setOnTouchListener(listener); button_today3.setOnTouchListener(listener); 
  • Immediately did not check, cheto does not want to cooperate code) I can not fix it. Zhmyak - Pollux
  • changed the answer, it was necessary so ((Button) findViewById (v.getId ())). setPressed (true); - Kirill Stoianov