0

Say I were to press a button twice, and I wanted a returned value of the amount of time between the two presses? Most sources tell me to use a chronometer but I don't want the time to be displayed anywhere on the app.

I apologize if this is a simple problem, I am relatively new to coding.

Nick Nagy
  • 61
  • 4

2 Answers2

0

I don't get this:

but I don't want the time to be displayed anywhere on the app.

Then don't display it. You have few class like Time or Date (or my fav lib JodaTime) that works with time and you can easily calculate interval in millis.

JoKr
  • 4,976
  • 8
  • 27
  • 39
0

I assume that you want to show the time some where but don't want to show it on your layout, then you can use a toast to do it.

public class MainActivity extends Activity {

    Button button;

    Long currentTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentTime != null) {
                    Long pastTime = System.currentTimeMillis() - currentTime;
                    Toast.makeText(
                            getApplicationContext(), "Time: " + pastTime, Toast.LENGTH_SHORT)
                            .show();
                }
                currentTime = System.currentTimeMillis();
            }
        });
    }
}

Read more: http://developer.android.com/guide/topics/ui/notifiers/toasts.html

Andyccs
  • 520
  • 3
  • 9