java - Create a Countdown Timer in Android Action Bar -
i'm having trouble creating countdown timer viewed action bar. right menu_main.xml , part of mainactivity.java code:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); undovaluesa.add(scoreteama); undovaluesb.add(scoreteamb); if (savedinstancestate != null) { scoreteama = savedinstancestate.getint(score_team_a_key); scoreteamb = savedinstancestate.getint(score_team_b_key); undovaluesa = savedinstancestate.getintegerarraylist(undo_a_key); undovaluesb = savedinstancestate.getintegerarraylist(undo_b_key); } displayforteama(scoreteama); displayforteamb(scoreteamb); swish = mediaplayer.create(this, r.raw.swish); textviewtime = (textview) findviewbyid(r.id.timer); textviewtime.settext("3:00"); } <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".mainactivity"> <item android:title = "0:00" android:id = "@+id/timer" app:showasaction = "always" /> <item android:id="@+id/action_plus" android:title="plus" android:icon="@drawable/ic_action_plus" android:orderincategory="100" app:showasaction="always"/> <item android:id="@+id/action_minus" android:title="minus" android:icon="@drawable/ic_action_minus" android:orderincategory="100" app:showasaction="always"/>
my app crashing because timer in action bar isn't textview, merely item. so, when try use textviewtime.settext("3:00"); crashes upon start. how implement this?
step #1: implement "count-down timer" custom view
class, such reversechronometer
implementation:
/*** copyright (c) 2013 commonsware, llc licensed under apache license, version 2.0 (the "license"); may not use file except in compliance license. may obtain copy of license @ http://www.apache.org/licenses/license-2.0. unless required applicable law or agreed in writing, software distributed under license distributed on "as is" basis, without warranties or conditions of kind, either express or implied. see license specific language governing permissions , limitations under license. _the busy coder's guide android development_ http://commonsware.com/android */ package com.commonsware.android.preso.slides; import android.content.context; import android.graphics.color; import android.os.systemclock; import android.util.attributeset; import android.widget.textview; public class reversechronometer extends textview implements runnable { long starttime=0l; long overallduration=0l; long warningduration=0l; boolean isrunning=false; public reversechronometer(context context) { super(context); reset(); } public reversechronometer(context context, attributeset attrs) { super(context, attrs); reset(); } @override public void run() { isrunning=true; long elapsedseconds= (systemclock.elapsedrealtime() - starttime) / 1000; if (elapsedseconds < overallduration) { long remainingseconds=overallduration - elapsedseconds; long minutes=remainingseconds / 60; long seconds=remainingseconds - (60 * minutes); settext(string.format("%d:%02d", minutes, seconds)); if (warningduration > 0 && remainingseconds < warningduration) { settextcolor(0xffff6600); // orange } else { settextcolor(color.white); } postdelayed(this, 1000); } else { settext("0:00"); settextcolor(color.red); isrunning=false; } } public void reset() { starttime=systemclock.elapsedrealtime(); settext("--:--"); settextcolor(color.white); } public void stop() { removecallbacks(this); isrunning=false; } public boolean isrunning() { return(isrunning); } public void setoverallduration(long overallduration) { this.overallduration=overallduration; } public void setwarningduration(long warningduration) { this.warningduration=warningduration; } }
step #2: use android:actionviewclass
(or, in case, app:actionviewclass
) point class step #1 in menu resource, such native action bar:
<item android:id="@+id/countdown" android:actionviewclass="com.commonsware.android.preso.slides.reversechronometer" android:showasaction="always" android:title="this should not needed"> </item>
step #3: retrieve action item , configure java code, such native action bar:
@override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_actions, menu); reversechronometer rc=(reversechronometer)menu .finditem(r.id.countdown) .getactionview(); rc.setoverallduration(40*60); rc.setwarningduration(5*60); rc.setonclicklistener(this); rc.setonlongclicklistener(this); rc.settextsize(typedvalue.complex_unit_sp, 24); rc.settextcolor(color.white); return(super.oncreateoptionsmenu(menu)); }
you need make appropriate adjustments step #2 , step #3, using appcompat-v7
action bar backport.
Comments
Post a Comment