Change text in button



import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn_apple = (Button) findViewById(R.id.button_apple);
    Button btn_cherry = (Button) findViewById(R.id.button_cherry);
    Button btn_orange = (Button) findViewById(R.id.button_orange);
    Button btn_waterLemon = (Button) findViewById(R.id.button_waterlemon);
    btn_apple.setOnClickListener(new View.OnClickListener() {
        boolean action = false;
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.button_apple:
                    if (!action) {
                        action = true;
                        btn_apple.setText("1");
                    }
                    else {
                        int i = Integer.parseInt(btn_apple.getText().toString());
                        btn_apple.setText(String.valueOf(i + 1));
                    }
                    break;
                case R.id.button_cherry:
                    action = false;
                    if (!action) {
                        action = true;
                        btn_cherry.setText("1");
                    }
                    else {
                        int i = Integer.parseInt(btn_cherry.getText().toString());
                        btn_cherry.setText(String.valueOf(i + 1));
                    }
                    break;
                }
            }
        });
    }
}

I need to make it so that if a user clicks on button_apple and doesn’t click on it for 3 seconds, its text becomes so superfluous. And if anyone knows why my text changes in button_apple, but not in button_cherry. Tell me, please.

Answer

You’re setting the listener on your apple button. The cherry button doesn’t have a listener on it.

Reconsider what you’re trying to achieve and simplify it.

If the user clicks the apple button you need to do something.

If the user clicks the cherry button you need to do something (maybe something else).

If the user clicks the watermellon button, … and so on.

// define your listeners
View.OnClickListener appleListener = new View.OnClickcListener() {
    @Override
    public void onClick(View v) {
        // do whatever you need to do when the apple button is clicked
    }
};

// same thing for cherry listener, make a listener to handle the click action
View.OnClickListener cherryListener = ...

// register the listeners
btnApple.setOnClickListener(appleListener);
btnCherry.setOnClickListener(cherryListener);

...

EDIT

To make something happen after a set amount of time you have to consider:

  1. the inputs: which objects / variables influence the action
  2. the output: what’s supposed to happpen if all inputs are valid
  3. the duration

With android you could use a handler. See the postDelayed method.

long delayMillis = 3000L; // duration after which to run your task

Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after the delay in milliseconds
  }
}, delayMillis);

All of this stuff has to be inside the click listener with the logic you need to implement.

“If a user clicks on button apple and doesn’t click on it for three seconds”

You could do something like

View.OnClickListener appleListener = new View.OnClickcListener() {
    AtomicInteger clicks = new AtomicInteger(0);

    @Override
    public void onClick(View v) {
        int numClicks = clicks.incrementAndGet();
        if (numClicks == 1) {
            long delayMillis = 3000L; // duration after which to run your task

            Handler handler = new Handler(Looper.getMainLooper());
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (clicks.get() == 1) {
                        // they only clicked once, do whatever you need to do to make the text superfluous
                    }
                    // put the number of clicks back to 0
                    clicks.set(0);
                }
            }, delayMillis);
        }
    } else {
        // TODO
        // it's been clicked more than once
        // show a toast if you need to or do something else
    }
}

I didn’t test this so you’ll probably have to modify it a bit but that’s the general idea.



Source: stackoverflow