-2

I want to remove the seconds of DigitalClock.

I found a code of DigitalCLock : http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/DigitalClock.java/?v=source

Now I use just DigitalClock like this:

DigitalClock dc = (DigitalClock) findViewById(R.id.digitalClock1);

How to use code from GrepCode ?

user1704195
  • 179
  • 1
  • 4
  • 16

2 Answers2

2

Because the formats for DigitalClock are private, the easiest way is to cut and paste the entire class into your project. Then make a couple changes:

  1. Remove ":ss" portion of each format, for example change m24 to "k:mm";.
  2. Lower the refresh rate in the Runnable from each second to each minute:

    long next = now + (60000 - now % 60000);
    

Now use it like any other widget (in XML scope it with your reversed package name):

<com.example.DigitalClock
    android:width="wrap_content"
    android:height="wrap_content" />
Sam
  • 86,580
  • 20
  • 181
  • 179
1

Adapted from the DigitalClock class you linked to.

package com.t3hh4xx0r.examplewidget

import android.content.Context;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.SystemClock;
import android.text.format.DateFormat;
import android.util.AttributeSet;

import java.util.Calendar;

public class CustomDigitalClock extends TextView {

    Calendar mCalendar;

    private Runnable mTicker;
    private Handler mHandler;

    private boolean mTickerStopped = false;

    String mFormat = "h:mm aa";

    public DigitalClock(Context context) {
        super(context);
        initClock(context);
    }

    public DigitalClock(Context context, AttributeSet attrs) {
        super(context, attrs);
        initClock(context);
    }

    private void initClock(Context context) {
        Resources r = mContext.getResources();

        if (mCalendar == null) {
            mCalendar = Calendar.getInstance();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        mTickerStopped = false;
        super.onAttachedToWindow();
        mHandler = new Handler();

        /**
         * requests a tick on the next hard-second boundary
         */
        mTicker = new Runnable() {
                public void run() {
                    if (mTickerStopped) return;
                    mCalendar.setTimeInMillis(System.currentTimeMillis());
                    setText(DateFormat.format(mFormat, mCalendar));
                    invalidate();
                    long now = SystemClock.uptimeMillis();
                    long next = now + (1000 - now % 1000);
                    mHandler.postAtTime(mTicker, next);
                }
            };
        mTicker.run();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mTickerStopped = true;
    }
}
r2DoesInc
  • 3,759
  • 3
  • 29
  • 60
  • not working example, here the good one http://stackoverflow.com/questions/7610549/android-digitalclock-remove-seconds – Zorb Sep 19 '13 at 17:03