Question

I want to build the app that selects the time from the time picker and then the time that is in text view and the count down timer starts and have hours ,mins, and seconds value... The Code is:-

      public class MainActivity extends Activity implements OnClickListener{
    /** Private members of the class */
    private TextView displayTime;
    private Button pickTime;
    private Button buttonStartTime, buttonStopTime;
    private TextView textViewShowTime; // will show the time
    private CountDownTimer countDownTimer; // built in android class
    private long totalTimeCountInMilliseconds; 
    private int pHour;
    private int pMinute;
    /** This integer will uniquely define the dialog to be used for displaying time picker.*/
    static final int TIME_DIALOG_ID = 0;

    /** Callback received when the user "picks" a time in the dialog */
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                pHour =pHour+ hourOfDay;
                pMinute = pMinute+minute;
                updateDisplay();
                displayToast();
            }
        };

    /** Updates the time in the TextView */
    private void updateDisplay() {
        displayTime.setText(
            new StringBuilder()
                    .append(pad(pHour)).append(":")
                    .append(pad(pMinute)));
 tv.setText(
                    new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mDay).append("-")
                    .append(mMonth + 1).append("-")

                    .append(mYear).append(" "));
    }

    /** Displays a notification when the time is updated */
    private void displayToast() {
        Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(displayTime.getText()),   Toast.LENGTH_SHORT).show();

    }

    /** Add padding to numbers less than ten */
    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** Capture our View elements */
        displayTime = (TextView) findViewById(R.id.timeDisplay);
        pickTime = (Button) findViewById(R.id.pickTime);

        buttonStartTime = (Button) findViewById(R.id.btnStartTime);
        buttonStopTime = (Button) findViewById(R.id.btnStopTime);
        displayTime = (TextView) findViewById(R.id.timeDisplay);

        buttonStartTime.setOnClickListener(this);
        buttonStopTime.setOnClickListener(this);


        /** Listener for click event of the button */
        pickTime.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });

        /** Get the current time */
        final Calendar cal = Calendar.getInstance();
        mYear = cal.get(Calendar.YEAR);
        mMonth = cal.get(Calendar.MONTH);
        mDay = cal.get(Calendar.DAY_OF_MONTH);
        pHour = cal.get(Calendar.HOUR_OF_DAY);
        pMinute = cal.get(Calendar.MINUTE);

        /** Display the current time in the TextView */
        updateDisplay();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnStartTime) {
           // textViewShowTime.setTextAppearance(getApplicationContext(),
                 //   R.style.normalText);
            setTimer();
            buttonStopTime.setVisibility(View.VISIBLE);
            buttonStartTime.setVisibility(View.GONE);
           //edtTimerValue.setVisibility(View.GONE);
            //edtTimerValue.setText("");
            startTimer();

        } else if (v.getId() == R.id.btnStopTime) {
            countDownTimer.cancel();
            buttonStartTime.setVisibility(View.VISIBLE);
            buttonStopTime.setVisibility(View.GONE);

        }
    }

    private void setTimer() {
        int times = 0;
        if (!displayTime.getText().toString().equals("")) {
           times = Integer.parseInt(displayTime.getText().toString());
            //time=timer;
        } else
            Toast.makeText(MainActivity.this, "Please Enter Minutes...",
                    Toast.LENGTH_LONG).show();

        totalTimeCountInMilliseconds = 60 * times * 1000;

        //timeBlinkInMilliseconds = 30 * 1000;
    }


    private void startTimer() {
        countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
            // 500 means, onTick function will be called at every 500
            // milliseconds

            @Override
            public void onTick(long leftTimeInMilliseconds) {
                long seconds = leftTimeInMilliseconds / 1000;



                displayTime.setText(String.format("%02d:%02d:%02d", seconds / 3600,
                        (seconds % 3600) / 60, (seconds % 60)));
                // format the textview to show the easily readable format

            }

            @Override
            public void onFinish() {
                // this function will be called when the timecount is finished
                displayTime.setText("Time up!");
                displayTime.setVisibility(View.VISIBLE);
                buttonStartTime.setVisibility(View.VISIBLE);
                buttonStopTime.setVisibility(View.GONE);
                //edtTimerValue.setVisibility(View.VISIBLE);
            }

        }.start();

    }

    /** Create a new dialog for time picker */

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, pHour, pMinute, false);
        }
        return null;
    }
}

It gives the invalid int error: because the value in text is '43:56' so how should i solve it

Was it helpful?

Solution

You don't need to get time from your displayTime. You need to get time from pHour and pMinute fields. Change this:

times = Integer.parseInt(displayTime.getText().toString());

to this:

times = pHour*60 + pMinutes;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top