Search code examples
androiddatetimeandroid-edittextonactivityresult

How to clear TimePicker editText?


In Activity B, I have two clickable editText(TI,TO), and one save Button. When EditText is clicked, TimePicker dialog will be shown. Save Button used to return this two value to Activity A.

The problem now is the EditText will display the latest value get from TimePicker dialog on Activity A even no value is selected on Activity B .

Let's said in Activity B , I have entered 1:00 on TI and 2:00 in TO. In second times, I didn't enter anything on the Activity B EditText and press button to return to A, it still will display 1:00 and 2:00 on Activity A.

Activity B

      timeIn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { //shows timePick
                    Fragment fragDialog = getSupportFragmentManager().findFragmentByTag("TimePicker");
                    if (fragDialog == null) { // Fragment not added
                        tp.setFlag(TimePick.FLAG_START_DATE);
                        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                        tp.show(ft, "TimePicker");
                    } else {

                        // already active
                    }
                }
            });

            timeOut.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Fragment fragDialog = getSupportFragmentManager().findFragmentByTag("TimePicker");
                    if(fragDialog == null)
                    {  // Fragment not added
                        tp.setFlag(TimePick.FLAG_END_DATE);
                        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                        tp.show(ft, "TimePicker");
                    }
                    else
                    {
                        // already active
                    }
                }
            });

  public static class TimePick extends android.support.v4.app.DialogFragment implements TimePickerDialog.OnTimeSetListener {

        public static final int FLAG_START_DATE = 00;
        public static final int FLAG_END_DATE = 01;
        private int flag = 00;

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);
            return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
        }

        public void setFlag(int i) {
            flag = i;
        }


        @Override
        public void onTimeSet(TimePicker view, int hourofDay, int minute) {

            if (flag == FLAG_START_DATE) {
                T1.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
                TimeIn = T1.getText().toString();
            }
            if (flag == FLAG_END_DATE) {
                TO.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
                TimeOut = TO.getText().toString();
            }

        }

  save.setOnClickListener(new View.OnClickListener()
        {  // return values to A activity
            @Override
            public void onClick(View v)
            {
                Intent returnIntent=new Intent();
                returnIntent.putExtra("TimeIn", TimeIn);
                returnIntent.putExtra("TimeOut",TimeOut);
                setResult(Activity.RESULT_OK,returnIntent);
                finish();

            }
        });

Activity A

       @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
                if (resultCode == RESULT_OK) {
                    if (requestCode == PROJECT_REQUEST_CODE) {
                        ReceiveTimeIn = data.getStringExtra("TimeIn");
                        ReceiveTimeOut = data.getStringExtra("TimeOut");
   Toast.makeText(getApplicationContext(),ReceiveTimeIn+ReceiveTimeOut,Toast.LENGTH_LONG).show();
           }

The Toast suppose to display null for the second time, but it display 1:00 2:00 instead.

Edited (Activity B)

  save.setOnClickListener(new View.OnClickListener() { // return values to  activity A

                 @Override
                 public void onClick(View v)
                 {
                      if (isEdited == false)
                      {
                          if(T1!=null||TO!=null) // if editText not clicked but has value pass from ListView A
                          {
                              // return T1 and TO value to listView Activity A
                          }
                          else { // if editText not clicked and no value passed, then only null
                              TimeIn = "";
                              TimeOut = "";
                          }
                      }

                 Intent returnIntent = new Intent();
                 Project = project.getSelectedItem().toString();
                 Description = description.getText().toString();
                 progress = seekBar.getProgress();
                 returnIntent.putExtra("Project", Project);
                 returnIntent.putExtra("Description", Description);
                 returnIntent.putExtra("progress", progress);
                 returnIntent.putExtra("TimeIn", TimeIn);
                 returnIntent.putExtra("TimeOut", TimeOut);
                 setResult(Activity.RESULT_OK, returnIntent);
                 finish();
                 }
          });

Solution

  • After the findViewById of both timeIn and timeOut edittext set them empty as follows:

    timeIn.setText(""); 
    timeOut.setText(""); 
    

    And your other two variable also:

    TimeIn = "";
    TimeOut = "";