How to check if Button is still clicked after second - java

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!

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.

Why not use the setOnLongClickListener function?
This should solve your issue.
An example can be find here: setOnLongClickListener

Related

Handling the entry and exit of a finger in the button area

There is a gButton button and there is an onTouch listener:
gButton.setOnTouchListener (new View.OnTouchListener () {
public boolean onTouch (View v, MotionEvent event) {
if (event.getAction () == MotionEvent.ACTION_DOWN) {
 ...
} else if (event.getAction () == MotionEvent.ACTION_UP) {
...
}
return false;
}
});
Something is done with ACTION_DOWN and ACTION_UP (pressing the button / releasing), but the pressing event occurs only when the button is touched, and if the button was pressed and you take your finger away, nothing will happen (there is no finger tap)
I tried to add below, after ACTION_DOWN, other "else if" conditions like ACTION_OUTSIDE (I took everything from the page: https://developer.android.com/reference/android/view/MotionEvent.html), but nothing happened. And yes, I used If, not Switch
I need to make sure that when I touch the button (if the finger is in the button area at all, even if the first touch was somewhere in the empty area and the finger was moved to the button), some actions were performed (for example, a Toast method with the text "On "), when you release your finger from the button area or when you move your finger from the button area, other actions were performed (Toast with the value of Off)
I tried to do this with one of the answers to a similar question by correcting it in my code: Cancel button press if user moves finger off button, however, an error prntscr.com/oghnm2 is displayed, and I do not know where and how to get L, R, T, B, except getTop();
I hope this might help,here i had checked mouse pointer location with the btn coordinates, if its location within range printed logs otherwise printed out of area.
btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(v.getLeft()<=event.getX() && v.getRight()>=event.getX() && v.getTop()<=event.getY() && v.getBottom()>=event.getY()) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
Log.d("LOG", "pressed");
if (event.getAction() == MotionEvent.ACTION_UP)
Log.d("LOG", "released");
if (event.getAction() == MotionEvent.ACTION_MOVE)
Log.d("LOG", "moving");
}
else
{
Log.d("LOG", "out of area");
}
return false;
}
});

How do I do a continuous job when I press and hold until it's released then it stops?

I have a button that when clicked it will increase my mediaplayer for 10s but I want to press and hold it will increase continuously until released.
btn10s.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.seekTo(mp.getCurrentPosition() + 10000);
}
});
Thank !
The method onClick works when you touch and release the button.
But when you want to do something in an event of touching the button,
you need the method called
onTouch (View v, MotionEvent event){}

Android ACTION_UP does not get fired after ACTION_MOVE

I'm trying to create a view programmatically, and change its background according to its click state.
I know I can do this with xml drawables, but I want to learn to program it too.
But if I press on my view, then slide my finger, the view gets stuck in pressed state. (white = 0xaaffffff background)
My code is this :
Edit:
I solved the problem. Here is the working code :
mainContainer.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
if (action==MotionEvent.ACTION_DOWN)
{
v.setBackgroundColor(0xaaffffff);
return false;
}
if(action==MotionEvent.ACTION_MOVE)
{
}
if(action==MotionEvent.ACTION_CANCEL)
{
v.setBackgroundColor(0x00ffffff);
}
if (action==MotionEvent.ACTION_UP)
{
v.setBackgroundColor(0x00ffffff);
return true;
}
return false;
}
});
As you see, I tried returning true wherever I want onTouch() to be called again. But apparently there is something I do not understand here.
What am I doing wrong ?
Thanks for any help.
What about returning true in your ACTION_UP aswell?
if (action==MotionEvent.ACTION_UP)
{
v.setBackgroundColor(0x00ffffff);
return true;
}

Basic Android : how to handle isPressed action on a button in Android

I wrote a working code using isEnabled().
if(btn.isEnabled()){
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// say send a udp packet
}
}
);
}
Now instead of the packet to be sent when the button is clicked, I want to send it when it stays pressed. How do I handle this?
when i tried isPressed instead of isEnabled, there was a blank screen and the activity was not even displayed...
EDIT : also tried btn.isPressed() - it doesn't work ... the udp packet gets sent immediately after I click on the button... I want it to send ONLY when I am pressing it ...
Any help would be appreciated.
Thanks
your condition is vague. "ONLY when I am pressing it" would mean you'll start sending when the button starts being pressed, which would mean on MotionEvent.ACTION_DOWN. if you want some delay before the action gets executed, create a timer thread that would start when MotionEvent.ACTION_DOWN is detected, and will execute your action after a few seconds. the timer should also reset when MotionEvent.ACTION_UP is detected, or if the action is already in progress, interrupt the action.
but honestly, you may want to rephrase your condition.
Not sure if this will work, but worth a try
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.isPressed()){
//do sth.
}
else{
//do sth. else
}
}
Your going to want to use the onTouchListener instead of onClick and stuff. Small change you also want to track when the user lets go.
EDIT adding timer stuff
Timer timer;
UDP request;
btn.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_DOWN){
//TODO start sending udp in background
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
request.start();
}
},DELAY_MS);
}
if(event.getAction() == MotionEvent.ACTION_UP){
//TODO stop sending udp
timer.cancel()
if(request.isTransmitting()){
request.stop();
}
}
//needed to get both calls
return true;
}
});

Changing background of button depending on current background

I'm trying to change the background of a button when it's clicked but also be able to change it back when it's clicked again. What I'm trying to do is get the buttons current background and if it is (xxxxx) then change it to (yyyyy). I can't seem to find how to get the button's current background and compare it to one of my drawable resources.
Any help would be much appreciated.
Some pseudo code of what I'm trying to do:
if (button1.getBackground() == R.drawable.someDrawable) {
button1.setBackgroundResource(R.drawable.someOtherDrawable);
}
I think this is the right way: link
I simple solution will be to have two drawables for background. One for non_pressed_selector and pressed_selector and just keep track if the button is pressed or not.
private boolean isPressed = false;
public void onClick() {
if (isPressed) {
// set not_pressed_selector
} else {
// set pressed_selector
}
isPressed != isPressed;
}
It seems that you need a ToggleButton instead of simple Button. You could use isChecked() method to determine the state of your button in OnClickListener and set your background there. Or you can define selector in xml as pedr0 said.
What I did was:
Declare the button in it's initial state:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/StateA"/>
</selector>
Then I manage the events from the code controling the actual state of the button with a tag:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.drawables_buttons); //This is the layout where it's the button
threeStateButton = (Button)findViewById(R.id.three_States_Button); //This is the button
threeStateButton.setOnTouchListener(new CustomTouchListener());
threeStateButton.setTag("StateA"); //Set the control tag
}
private class CustomTouchListener implements View.OnTouchListener
{
#Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction())
{
case MotionEvent.ACTION_UP: //When you lift your finger
if (threeStateButton.getTag().equals("StateA"))
{
threeStateButton.setBackgroundResource(R.drawable.StateB);
Toast.makeText(view.getContext(), "This gonna change my state from StateA to StateB",Toast.LENGTH_SHORT).show();
threeStateButton.setTag("StateB");
}
else //If when you lift your finger it was already on stateB
{
threeStateButton.setBackgroundResource(R.drawable.red_button);
Toast.makeText(view.getContext(), "This gonna change my state from StateB to StateA",Toast.LENGTH_SHORT).show();
threeStateButton.setTag("StateA");
}
break;
//In case you want that you button shows a different state when your finger is pressing it.
case MotionEvent.ACTION_DOWN:
threeStateButton.setBackgroundResource(R.drawable.StateButtonPressed);
break;
}
return false;
}
}
I don't know if this is the best way to do it but it works, and yes, I'd like to know wich is the optimal way.

Categories

Resources