android - How to make it from vertical to horizontal view -
when activate start time see number being increased during time when change mobile landscape vertical horizontal , horizontal vertical.
obstacle:
when number displayed increased value , want change mobile landscape vertical horizontal view. when did itthe numbers go scratch.
today, i'm newbie in android , source code below doesn't work make it.
what part missing?
-------------------------- class stopwatchactivity package com.jfdimarzio.stopwatch4; import android.os.handler; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.textview; public class stopwatchactivity extends appcompatactivity { private int seconds = 0; private boolean running; private boolean wasrunning; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_stopwatch); if(savedinstancestate != null) { seconds = savedinstancestate.getint("seconds"); running = savedinstancestate.getboolean("running"); wasrunning = savedinstancestate.getboolean("wasrunning"); } runtimer(); } @override public void onsaveinstancestate(bundle savedinstancestate) { savedinstancestate.putint("seconds", seconds); savedinstancestate.putboolean("running", running); savedinstancestate.putboolean("wasrunning", wasrunning); } @override protected void onstop() { super.onstop(); wasrunning = running; running = false; } @override protected void onstart() { super.onstart(); if(wasrunning) { running = true; } } @override protected void onpause() { super.onpause(); wasrunning = running; running = false; } @override protected void onresume() { super.onresume(); if (wasrunning) { running = true; } } public void onclickstart(view view) { running = true; } public void onclickstop(view view) { running = false; } public void onclickreset(view view) { running = false; seconds= 0; } private void runtimer() { final textview timeview = (textview)findviewbyid(r.id.time_view); final handler handler = new handler(); handler.post(new runnable() { @override public void run() { int hours = seconds/3600; int minutes = (seconds%3600)/60; int secs = seconds%60; string time = string.format("%d:%02d:%02d", hours, minutes, secs); timeview.settext(time); if(running) { seconds++; } handler.postdelayed(this, 1000); } }); } } ------------------------------ activity_stopwatch.xml <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_stopwatch" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.jfdimarzio.stopwatch4.stopwatchactivity"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!" android:id="@+id/textview" /> <textview android:id="@+id/time_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="0dp" android:text="" android:textappearance="?android:attr/textappearancelarge" android:textsize="92sp" /> <button android:id="@+id/start_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/time_view" android:layout_centerhorizontal="true" android:layout_margintop="20dp" android:onclick="onclickstart" android:text="@string/start" /> <button android:id="@+id/stop_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/start_button" android:layout_centerhorizontal="true" android:layout_margintop="10dp" android:onclick="onclickstop" android:text="@string/stop" /> <button android:id="@+id/reset_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/stop_button" android:layout_centerhorizontal="true" android:layout_margintop="10dp" android:onclick="onclickreset" android:text="@string/reset" /> </relativelayout> ------------------------------ strings.xml <resources> <string name="app_name">stopwatch4</string> <string name="start">start</string> <string name="stop">stop</string> <string name="reset">reset</string> </resources>
you use savedinstancestate.putint("seconds") savedinstancestate.getint("seconds"); change "seconds" "seconds" inside oncreate().
hope help, i'm new stackoverflow.
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_stopwatch); if(savedinstancestate != null) { seconds = savedinstancestate.getint("seconds"); running = savedinstancestate.getboolean("running"); wasrunning = savedinstancestate.getboolean("wasrunning"); } runtimer(); }
Comments
Post a Comment