java - Wrong updating TextView in Android app -
i create simple android app (https://www.linux.com/learn/docs/683628-android-programming-for-beginners-part-1) latest android studio. code:
public class test_act extends activity { private static final int millis_per_second = 1000; private static final int seconds_to_countdown = 30; private android.widget.textview countdowndisplay; private android.os.countdowntimer timer; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.full_act); countdowndisplay = (android.widget.textview) findviewbyid(r.id.time_display_box); android.widget.button startbutton = (android.widget.button) findviewbyid(r.id.startbutton); startbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { try { showtimer(seconds_to_countdown * millis_per_second); } catch (numberformatexception e) { // method ignores invalid (non-integer) input , waits // can use } } }); } private void showtimer(int countdownmillis) { if(timer != null) { timer.cancel(); } timer = new android.os.countdowntimer(countdownmillis, millis_per_second) { @override public void ontick(long millisuntilfinished) { countdowndisplay.settext("counting down: " + millisuntilfinished / millis_per_second); } @override public void onfinish() { countdowndisplay.settext("kaboom!"); } }.start(); } }
my xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/time_display_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="60dp" android:text="@string/_00_30" android:textappearance="?android:attr/textappearancelarge"/> <button android:id="@+id/startbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/time_display_box" android:layout_centerhorizontal="true" android:layout_margintop="41dp" android:text="@string/start" /> </relativelayout>
in emulator it's working. on galaxy s2 cyanogenmod10.1(android 4.2.2) app wrong updating textview. screenshot:
how can resolve problem?
upd: after screen rotate textview updating once.
you might want try invalidating layout every time updated. guessing how text being updated phone not having enough time redraw layout. explain why works when rotate phone, because layout forced update.
countdowndisplay.invalidate();
let me know if not work.
Comments
Post a Comment