Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Reset on counter?

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reset on counter?

    Hey there!

    I've been experimenting with Eclipse a bit...
    and I almost finished a timer
    Although, I can't get the Reset button working...

    Res/Layout/main.xml
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="match_parent" >
    <TextView
    android:id="@+id/timerValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/pauseButton"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="37dp"
    android:textSize="40sp"
    android:textColor="#ffffff"
    android:text="@string/timerVal" />

    <Button
    android:id="@+id/startButton"
    android:layout_width="90dp"
    android:layout_height="45dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="38dp"
    android:text="@string/startButtonLabel" />

    <Button
    android:id="@+id/pauseButton"
    android:layout_width="90dp"
    android:layout_height="45dp"
    android:layout_alignBaseline="@+id/startButton"
    android:layout_alignBottom="@+id/startButton"
    android:layout_alignParentRight="true"
    android:layout_marginRight="38dp"
    android:text="@string/pauseButtonLabel" />

    <Button
    android:id="@+id/resetButton"
    android:layout_width="90dp"
    android:layout_height="45dp"
    android:layout_above="@+id/timerValue"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="31dp"
    android:text="@string/resetButtonLabel" />

    </RelativeLayout>
    res/values/strings.xml
    <?xml version="1.0" encoding="utf-8"?>

    <resources>

    <string name="app_name">Timer</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="timerVal">00:00:00</string>
    <string name="pauseButtonLabel">Pause</string>
    <string name="startButtonLabel">Start</string>
    <string name="resetButtonLabel">Reset</string>
    </resources>
    MainActivity.java
    package com.MertensMobile.Timer;

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.SystemClock;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class MainActivity extends Activity {

    private Button startButton;
    private Button pauseButton;

    private TextView timerValue;

    private long startTime = 0L;

    private Handler customHandler = new Handler();

    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedTime = 0L;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    timerValue = (TextView) findViewById(R.id.timerValue);
    startButton = (Button) findViewById(R.id.startButton);
    startButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
    startTime = SystemClock.uptimeMillis();
    customHandler.postDelayed(updateTimerThread, 0);
    }
    });

    pauseButton = (Button) findViewById(R.id.pauseButton);

    pauseButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
    timeSwapBuff += timeInMilliseconds;
    customHandler.removeCallbacks(updateTimerThread);
    }
    });

    resetButton = (Button) findViewById(R.id.resetButton);

    resetButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
    timeSwapBuff += timeInMilliseconds;
    customHandler.removeCallbacks(updateTimerThread);
    }
    });

    }
    private Runnable updateTimerThread = new Runnable() {
    public void run() {

    timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
    updatedTime = timeSwapBuff + timeInMilliseconds;
    int secs = (int) (updatedTime / 1000);
    int mins = secs / 60;
    secs = secs % 60;
    int milliseconds = (int) (updatedTime % 1000);
    timerValue.setText("" + mins + ":"
    + String.format("%02d", secs) + ":"
    + String.format("%03d", milliseconds));
    customHandler.postDelayed(this, 0);
    }
    public void Reset() {
    Intent intent = getIntent();
    overridePendingTransition(0, 0);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION) ;
    finish();

    overridePendingTransition(0, 0);
    startActivity(intent);
    }

    };

    }

    I'm very close, (at least I think I am)

    I got it working on my cellphone, just the reset isn't working yet


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Reset on counter?

    resetButton = (Button) findViewById(R.id.resetButton);
     
    resetButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);
        }
    });

    Where are you resetting the time?

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reset on counter?

    Well I've tried several things, but I'm just not sure what code I should enter for it

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Reset on counter?

    The purpose of a reset button is a timer is to reset the time no? You could just set the TextView back to 00:00:00

    Also; cross posted here. Please see The problems with cross posting.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reset on counter?

    I'll need some more help on this one...
    Last edited by Snake-KoRn; July 21st, 2014 at 10:21 AM.

  6. #6
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Reset on counter?

    It looks like you are declaring the startButton and pauseButton as member variables of the class, but not the resetButton so it's only scoped for the onCreate method.

    Wait, hold on... It's not declared at all!! This line should be a syntax error.

    pauseButton = (Button) findViewById(R.id.pauseButton);

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reset on counter?

    Yeah, I noticed.
    I added private Button resetButton;on top. (is that what you mean?)

    --- Update ---

    Please just explain me what to do, I can't find any good and correct tutorial on the web for this .......

  8. #8
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Reset on counter?

    As I said in your last thread, tutorials are okay but check the Android docs. It's your primary source of information.

    As I also said in your last thread, we are here to help you (which you make difficult by editing your old posts like that) .. not solve your problems for you.

    What exactly is your error. What does the IDE tell you about the syntax errors? If it compiles and crashes, what does the logcat tell you?

  9. #9
    Junior Member
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reset on counter?

    -
    Last edited by Snake-KoRn; July 21st, 2014 at 01:26 PM.

Similar Threads

  1. [SOLVED] TicTacToe reset problems
    By gamerlovo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 13th, 2014, 07:45 PM
  2. Reset button.
    By ish in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 07:57 AM
  3. Why does this code reset at every paragraph?
    By sungkhum in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 10th, 2011, 06:36 PM
  4. Arcade scores reset
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: July 3rd, 2010, 05:31 PM
  5. Reset the value of each variable
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 22nd, 2009, 09:45 AM