0

I have a count down timer with a TextView (textCountdown) showing the time counting down. The user can click on a “Set Time” button and a TimePickerDialog box appears.

I would like to have the same textCountdown in my TimePickerDialog box so that the user can see the count down in the Dial box.

When I use the timePickerDialog.setMessage command the time that appears is the time when the user pressed the “Set Time” Button and the TimePickerDialog box is created. So the timePickerDialog.setMessage command displays a static text.

I would like it to be dynamic and display the Time counting down.

I uploaded an image to better understand what I mean: Count Down Timer

  btnDialBoxTime.setOnClickListener(new OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {


            final CustomTimePickerDialog timePickerDialog = new CustomTimePickerDialog(AdvancedCDT.this, TimePickerDialog.THEME_DEVICE_DEFAULT_DARK, timeSetListener, 0, 1, true);

            timePickerDialog.setTitle("Add Time");

            timePickerDialog.setMessage("" + -hourse + ":" + "" + String.format("%02d", -minutes) + ":" + String.format("%02d", -seconds) + ":" + String.format("%03d", -milliseconds));
.    
.    
.               
public static class CustomTimePickerDialog extends TimePickerDialog
      {
          public CustomTimePickerDialog (Context arg0,  int theme, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)
        {
              super(arg0, theme, callBack, hourOfDay, minute, is24HourView);

        }
public int getMinute(int minute)
{
  return minute;


   }
  }

    .
    .
    .
private CustomTimePickerDialog.OnTimeSetListener timeSetListener = new CustomTimePickerDialog.OnTimeSetListener()
          {
            @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute)
                {
        // Do some things.....

            }
          }; 

The code for the timer:

private Runnable updateTimerMethod = new Runnable()
      {
        public void run()
        {

         timeInMillies = SystemClock.uptimeMillis() - startTimeMil - startTime ;
         finalTime =  timeSwap + timeInMillies;

          seconds = ((int)(finalTime / 1000L));
          minutes = seconds / 60;

          seconds %= 60;

          hourse = (minutes / 60);


          int milliseconds = (int) (finalTime % 1000);
          int i = (int)(finalTime % 1000L);


          textCountdown.setText("" + -hourse + ":" + "" + String.format("%02d", -minutes) + ":" + String.format("%02d", -seconds) + ":" + String.format("%03d", -milliseconds));

          myHandler.postDelayed(this, 0L);


        }
      };

UPDATE 1----------------------

So I tried this:

final CustomTimePickerDialog timePickerDialog = new CustomTimePickerDialog(AdvancedCDT.this, TimePickerDialog.THEME_DEVICE_DEFAULT_DARK, timeSetListener, 0, 1, true);

    customCDT.setText("" + String.format("%01d", counterHoure + counterHoureDial) + ":" + "" + String.format("%02d", counterFiveMin + counterMinDial) + ":" + String.format("%02d", -seconds) + ":" + String.format("%03d", milliseconds));

            FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
            fl.addView(customCDT, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

I get a NullPoiterException. I have also seen this Custom AlertDialog... but it's not helping.

I also tried to use timePickerDialog.setCustomTitle(textCountdown); but I get:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Is there no way to do it without creating a custom layout? Can I use maybe a the: addTextChangedListener on the TextView ?

UPDATE 2--------------

Added logCat for NullPoiterException. I do not have a custom.xml layout.

06-10 15:40:48.564: D/dalvikvm(6675): GC_FOR_ALLOC freed 18K, 1% free 23584K/23636K, paused 13ms, total 13ms
06-10 15:40:48.584: I/dalvikvm-heap(6675): Grow heap (frag case) to 37.250MB for 14878736-byte allocation
06-10 15:40:48.604: D/dalvikvm(6675): GC_FOR_ALLOC freed 1K, 1% free 38113K/38168K, paused 14ms, total 14ms
06-10 15:40:48.814: E/MediaPlayer(6675): Should have subtitle controller already set
06-10 15:40:51.217: D/AndroidRuntime(6675): Shutting down VM
06-10 15:40:51.217: W/dalvikvm(6675): threadid=1: thread exiting with uncaught exception (group=0x41c06ba8)
06-10 15:40:51.227: E/AndroidRuntime(6675): FATAL EXCEPTION: main
06-10 15:40:51.227: E/AndroidRuntime(6675): Process: com.example.marios_splash, PID: 6675
06-10 15:40:51.227: E/AndroidRuntime(6675): java.lang.NullPointerException
06-10 15:40:51.227: E/AndroidRuntime(6675):     at com.example.marios_splash.AdvancedCDT$7.onClick(AdvancedCDT.java:204)
06-10 15:40:51.227: E/AndroidRuntime(6675):     at android.view.View.performClick(View.java:4438)
06-10 15:40:51.227: E/AndroidRuntime(6675):     at android.view.View$PerformClick.run(View.java:18422)
06-10 15:40:51.227: E/AndroidRuntime(6675):     at android.os.Handler.handleCallback(Handler.java:733)
06-10 15:40:51.227: E/AndroidRuntime(6675):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-10 15:40:51.227: E/AndroidRuntime(6675):     at android.os.Looper.loop(Looper.java:136)
06-10 15:40:51.227: E/AndroidRuntime(6675):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-10 15:40:51.227: E/AndroidRuntime(6675):     at java.lang.reflect.Method.invokeNative(Native Method)
06-10 15:40:51.227: E/AndroidRuntime(6675):     at java.lang.reflect.Method.invoke(Method.java:515)
06-10 15:40:51.227: E/AndroidRuntime(6675):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-10 15:40:51.227: E/AndroidRuntime(6675):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-10 15:40:51.227: E/AndroidRuntime(6675):     at dalvik.system.NativeStart.main(Native Method)

I was able to use this: TextView clone

    TextView cloned = new TextView(getApplicationContext());
    cloned.setText(textCountdown.getText());
    cloned.setLayoutParams(textCountdown.getLayoutParams());

    timePickerDialog.setCustomTitle(cloned);

but the title is still static and updating only once clicked.

Community
  • 1
  • 1
M_Bar
  • 1
  • 2

1 Answers1

0

Try this. I haven't, though I guess it would work.

    @SuppressWarnings("deprecation")
    public void onClick(View v) {

        final CustomTimePickerDialog timePickerDialog = new CustomTimePickerDialog(AdvancedCDT.this, TimePickerDialog.THEME_DEVICE_DEFAULT_DARK, timeSetListener, 0, 1, true);
        timePickerDialog.setTitle("Add Time");
        FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
        fl.addView(textCountdown, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
    }

What you basically need to do is to set a custom layout to the dialog. Initializing the FrameLayout and adding your textView should do it. I don't know if the handler would still be able to update it, but it is worth a try.

Look at this doc : http://developer.android.com/reference/android/app/AlertDialog.html

All the best.

Anudeep Bulla
  • 8,318
  • 4
  • 22
  • 29