Search code examples
javaandroid-studiocountdowntimer

Using Edittext for Countdown timer


i have find a code for countdown timer but the value is 10 sec. i want it to get the values from Edittext this is the code

private static final long TIMER_LENGHT = 10; // Ten seconds
    private long mTimeToGo;
    private CountDownTimer mCountDownTimer;
    private TimerState mState;

i did that

Edittext1 et1 = (EditText)findViewById(R.id.Edittext1);
String strInput = et1.getText().toString();
private static final long TIMER_LENGHT = et1; 
private long mTimeToGo;
private CountDownTimer mCountDownTimer;
private TimerState mState;

but it did not work what can i do to make it work *this is the Full code i'm using


Solution

  • Shouldn't this :

    private static final long TIMER_LENGHT = et1;
    

    be:

    private static final long TIMER_LENGHT = Long.parseLong(strInput);
    

    EDIT:

    First change your TIMER_LENGTH TO:

      private long TIMER_LENGHT = 0;
    

    We'll start it off as 0 and later when we click the button we change it:

    @OnClick(R.id.main_timer_button)
        public void onButtonClicked() {
            if  (mState == TimerState.STOPPED) {
                mPreferences.setStartedTime(getNow());
                EditText et1 = (EditText)findViewById(R.id.et1);
                String strInput = et1.getText().toString().trim();
                TIMER_LENGHT = Long.parseLong(strInput);
                startTimer();
                mState = TimerState.RUNNING;
            }
        }