2

When i try to show timer on textView with setText method, it is working. But here timer (text) does not get any update, shows only "0, 0, 0, 0" on card. I'm using cardslib library to show cards. I don't know if it is possible to show timer on cards. Can anyone help me to overcome this?

*This is my code:

long day, hour, min, sec;
MyCard card;

        ArrayList<Card> cards = new ArrayList<Card>();
        card = new MyCard(getActivity());
        card.mainHeader= "Main Header";
        card.mainTitle = "Main Title";

    new CountDownTimer(86500000, 1000) { // adjust the milli seconds here

        public void onTick(long millisUntilFinished) {

        card.secondaryTitle = day + " day, " + hour + " hour, " + min + " minute, " + sec + " second";
            day = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
            hour = TimeUnit.MILLISECONDS.toHours(millisUntilFinished) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millisUntilFinished));
            min = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished));
            sec = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished));

        }
        public void onFinish() {card.secondaryTitle = ("done!");}
    }.start();

        card.resourceIdThumb = R.drawable.ic_school_black_48dp;

        CardHeader header = new CardHeader(getActivity());
        header.setTitle(card.mainHeader);
        //Add Header to card
        card.addCardHeader(header);

        //Add the thumbnail
        CardThumbnail thumb = new CardThumbnail(getActivity());
        thumb.setDrawableResource(card.resourceIdThumb);
        card.addCardThumbnail(thumb);

        card.setOnClickListener(listener);

        cards.add(card);

Sorry i couldn't upload image because of my reputation. This is the link for picture of my card:

http://tinypic.com/view.php?pic=a2vdx5&s=8#.VeShM_btmko

Fatih Turgut
  • 319
  • 3
  • 9

3 Answers3

1

You could create a method that update it every 1 second. You could do as follows take a look :

  Thread t = new Thread() {

  @Override
  public void run() {
    try {
      while (!isInterrupted()) {
        Thread.sleep(1000);
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            // SetText here
          }
        });
      }
    } catch (InterruptedException e) {
    }
  }
};

t.start();

EDIT

Create a method that update the card as follows :

public void UpdateCard(){
card.secondaryTitle = day + " day, " + hour + " hour, " + min + " minute, " + sec + " second";
day = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
hour = TimeUnit.MILLISECONDS.toHours(millisUntilFinished) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millisUntilFinished));
min = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished));
sec = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished));
}

And inside of the run() call this method.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
1

I solved my problem by using setupInnerViewElements method in cardslib. I put CountDownTimer class into this method and it's worked. This is my java code:

    public MyCard1(Context context) {
            super(context, R.layout.home_card_inner_content1);
    }

    @Override
    public void setupInnerViewElements(ViewGroup parent, View view) {

        TextView mTitleTextView1 = (TextView) parent.findViewById(R.id.home_card_main_inner_title1);
        final TextView mSecondaryTitleTextView1 = (TextView) parent.findViewById(R.id.home_card_main_inner_subtitle1);

            new CountDownTimer(86500000, 1000) { // adjust the milli seconds here

                public void onTick(long millisUntilFinished) {

                    day1 = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
                    hour1 = TimeUnit.MILLISECONDS.toHours(millisUntilFinished)- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millisUntilFinished));
                    min1 = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished));
                    sec1 = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished));
                    mSecondaryTitleTextView1.setText(day1 + " day, " + hour1 + " hour, " + min1 + " minute, " + sec1 + " second");
                }
                public void onFinish() {mSecondaryTitleTextView1.setText("done!");}
            }.start();

        if (mTitleTextView1 != null)
            mTitleTextView1.setText(mainTitle1);

        if (mSecondaryTitleTextView1 != null)
            mSecondaryTitleTextView1.setText(secondaryTitle1);
    }
}
Fatih Turgut
  • 319
  • 3
  • 9
0

Every Time processing (i mean, show to the user) it must be done in Threads in Java (and therefore on Android). Then you need to use a RUNNABLE, do not use TIMER TASKS (timer tasks on android are kind bugged). Create an attribute:

 private final Handler handler2 = new Handler();//As attribute e.g.

Made a runnable (as attribute):

private Runnable runnable1 = new Runnable() {
    @Override
    public void run() {
         //made your edittext changes here
         handler1.postDelayed(this, 100);//1000=1second, 100=1sec/10
         //postDelayed =  it is to recall the runnable
    }
};

To start this runnable, you have to call this when needed

handler1.postDelayed(runnable1, 100);

To stop this runnable, you have to call this when needed:

handler1.removeCallbacks(runnable1);
TroniPM
  • 329
  • 3
  • 13