4 Replies - 2593 Views - Last Post: 06 November 2012 - 06:57 AM

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Trivia game countdown timer

Posted 04 November 2012 - 02:32 PM

I am attempting to make a simple trivia game Android app. I have a main menu that leads to the first question and then the app moves to a new question after each correct answer.

I want to try and implement a countdown timer now but I haven't been able to figure it out all day. I know if has something to do with the android.os.CountDownTimer import.

Thank you in advance!

Q1.java
package com.example.test;

import com.example.test.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.onclickListener;

public class Q1 extends Activity {
	
	Button button;	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.q1main);
		addListenerOnButton();
	}
	
	public void addListenerOnButton() {
		
		final Context context = this;
		button = (Button) findViewById(R.id.q1b4);
		button.setonclickListener(new onclickListener() {
			
			public void onclick(View arg0) {
				Intent intent = new Intent(context, Q2.class);
				startActivity(intent);
			}
		});
	}
}



q1main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="top"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="false"
        android:layout_centerVertical="false"
        android:text="What is the name of the first man?" />
    
    <Button
        android:id="@+id/q1b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="60dp"
        android:text="Able" />

    <Button
        android:id="@+id/q1b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="130dp"
        android:text="Cain" />

    <Button
        android:id="@+id/q1b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="58dp"
        android:text="Eve" />


    <Button
        android:id="@+id/q1b4"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:text="Adam" />

</LinearLayout>



Is This A Good Question/Topic? 0
  • +

Replies To: Trivia game countdown timer

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 864
  • Posts: 2,652
  • Joined: 29-July 11

Re: Trivia game countdown timer

Posted 04 November 2012 - 03:01 PM

Let me guess, you don't fully understand what an abstract class is, so you have been having trouble trying to instantiate a new CountDownTimer, right?

You need to subclass it to use it. It is very similar to a Swing timer.

CountDownTimer timer = new CountDownTimer(Long.MAX_VALUE, 30) {
    public void onTick(Long millisUntilFinished) {
        //this is your game loop where you update the game
    }

    public void onfinish() {
        //the timer has finished.
    }
};



You call timer.start() to start the timer, and timer.cancel() to stop it. It's pretty simple.

This post has been edited by farrell2k: 04 November 2012 - 03:06 PM

Was This Post Helpful? 1
  • +
  • -

#3 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Trivia game countdown timer

Posted 05 November 2012 - 02:21 PM

Farrel, you hit the nail on the head. I am not familiar with abstract and subclasses.

I tried implementing what you gave me and it still just is not working. The program is crashing when it tries to switch to the activity with the timer. The app goes away and prompts me on the emulator that "The testing has unfortunately stopped."

Any further guidance would be appreciated.

Q1.java
package com.example.test;

import com.example.test.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.onclickListener;

public class Q1 extends Activity {
	
	Button button;
	TextView text;
	TextView timeElapsedView;
	Timer countDownTimer;
	
	long timeElapsed;
	final long startTime = 86957;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.q1main);
		addListenerOnButton();
		text = (TextView) this.findViewById(R.id.timer);
		timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
		text.setText(text.getText() + String.valueOf(startTime));
		countDownTimer.start();
	}
	
	public void addListenerOnButton() {
		
		final Context context = this;
		button = (Button) findViewById(R.id.q1b4);
		button.setonclickListener(new onclickListener() {
			
			public void onclick(View arg0) {
				Intent intent = new Intent(context, Q2.class);
				startActivity(intent);
			}
		});
	}
	
	public class Timer extends CountDownTimer {
		public Timer(long startTime, long interval) {
			super(startTime, interval);
		}
		
		@Override
		public void onTick(long millisUntilFinished) {
			text.setText("Time remaining: " + millisUntilFinished);
			timeElapsed = startTime - millisUntilFinished;
			timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
		}
		
		@Override
		public void onfinish() {
			text.setText("Time's up!");
			timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
		}
	}
}



q1main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="top"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="false"
        android:layout_centerVertical="false"
        android:text="What is the name of the first man?" />
    
    <TextView
        android:id="@+id/timer"
        android:text="Time: "
        android:paddingRight="40dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  />
        
    <TextView
        android:id="@+id/timeElapsed"
        android:text="Time elapsed: "
        android:paddingRight="80dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  />
    
    <Button
        android:id="@+id/q1b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="60dp"
        android:text="Able" />

    <Button
        android:id="@+id/q1b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="130dp"
        android:text="Cain" />

    <Button
        android:id="@+id/q1b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="58dp"
        android:text="Eve" />


    <Button
        android:id="@+id/q1b4"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:text="Adam" />

</LinearLayout>


Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 864
  • Posts: 2,652
  • Joined: 29-July 11

Re: Trivia game countdown timer

Posted 05 November 2012 - 06:28 PM

Well, it looks like your Q1 class has an inner class with the CountDownTimer, and I assume this is your main Activity, so you have me confused. Post your classes and your error output from LogCat, and me or someone else will help you figure it out.
Was This Post Helpful? 0
  • +
  • -

#5 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Trivia game countdown timer

Posted 06 November 2012 - 06:57 AM

Below are 2 classes and their xml files. One class (S1.java) is the "main menu" and the other (Q1.java) is the first question.

I would post the logcat output but I struggle with the Eclipse built-in emulator night and day to get it to function right and right now it hasn't loaded properly in about 12 hours. I will figure that out on my own somehow.

S1.java
package com.example.test;

import com.example.test.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.onclickListener;
 
public class S1 extends Activity {
 
	Button button;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.s1main);
		addListenerOnButton();
	}
 
	public void addListenerOnButton() {
 
		final Context context = this;
		button = (Button) findViewById(R.id.button1);
		button.setonclickListener(new onclickListener() {
 
			public void onclick(View arg0) {
			    Intent intent = new Intent(context, Q1.class);
                startActivity(intent);   
			}
		});
	}
}



Q1.java
package com.example.test;

import com.example.test.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.onclickListener;

public class Q1 extends Activity {
	
	Button button;
	TextView text;
	TextView timeElapsedView;
	Timer countDownTimer;
	
	long timeElapsed;
	final long startTime = 86957;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.q1main);
		addListenerOnButton();
		text = (TextView) this.findViewById(R.id.timer);
		timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
		text.setText(text.getText() + String.valueOf(startTime));
		countDownTimer.start();
	}
	
	public void addListenerOnButton() {
		
		final Context context = this;
		button = (Button) findViewById(R.id.q1b4);
		button.setonclickListener(new onclickListener() {
			
			public void onclick(View arg0) {
				Intent intent = new Intent(context, Q2.class);
				startActivity(intent);
			}
		});
	}
	
	public class Timer extends CountDownTimer {
		public Timer(long startTime, long interval) {
			super(startTime, interval);
		}
		
		@Override
		public void onTick(long millisUntilFinished) {
			text.setText("Time remaining: " + millisUntilFinished);
			timeElapsed = startTime - millisUntilFinished;
			timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
		}
		
		@Override
		public void onfinish() {
			text.setText("Time's up!");
			timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
		}
	}
}



s1main.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
        
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="An app to grow biblical knowledge."
        tools:context=".BIBLE TRIVIA"
    />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="START"
        android:id="@+id/button1"
    />
        
</LinearLayout>



q1main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="top"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="false"
        android:layout_centerVertical="false"
        android:text="What is the name of the first man?" />
    
    <TextView
        android:id="@+id/timer"
        android:text="Time: "
        android:paddingRight="40dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  />
        
    <TextView
        android:id="@+id/timeElapsed"
        android:text="Time elapsed: "
        android:paddingRight="80dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  />
    
    <Button
        android:id="@+id/q1b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="60dp"
        android:text="Able" />

    <Button
        android:id="@+id/q1b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="130dp"
        android:text="Cain" />

    <Button
        android:id="@+id/q1b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="58dp"
        android:text="Eve" />


    <Button
        android:id="@+id/q1b4"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:text="Adam" />

</LinearLayout>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1