Saqib S
Saqib S

Reputation: 541

How to take input from user while using CountDownTimer in android

Here at CountDownTimer below, I want to have a user input value in milli seconds instead of pre loaded value of 30000. Please anybody help in this case. I am not able to figure out myself.

public class MainActivity extends AppCompatActivity {

    TextView mTextField;
    EditText mEditText;
    Button mButton;


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

        mTextField = (TextView) findViewById(R.id.timerView);
        mButton = (Button) findViewById(R.id.startButton);


        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Turning of the button until the timer finishes
                mButton.setEnabled(false);
                mButton.setClickable(false);

                new CountDownTimer(30000, 1000) { // Here I want to have a user input value in milli seconds instead of pre loaded value of 30000

                    public void onTick(long millisUntilFinished) {
                        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                    }

                    public void onFinish() {
                        mTextField.setText("done!");
                        // Turning on the button when the timer has finnished
                        mButton.setEnabled(true);
                        mButton.setClickable(true);
                    }
                }.start();


            }
        });

    }

}**strong text**

Upvotes: 1

Views: 1192

Answers (1)

Aakash
Aakash

Reputation: 5251

It is very simple, give any option for user input like an edit text and then pass those values as parameters in the CountDownTimer.

// 3000 and 200 will be input from user
long millisinFuture= 3000;
long delayTime=200;

new CountDownTimer(millisinFuture, delayTime) { // Here I want to have a user input value in milli seconds instead of pre loaded value of 30000

                public void onTick(long millisUntilFinished) {
                    mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                }

                public void onFinish() {
                    mTextField.setText("done!");
                    // Turning on the button when the timer has finnished
                    mButton.setEnabled(true);
                    mButton.setClickable(true);
                }
            }.start();

Upvotes: 2

Related Questions