package com.android.tapme;
import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.view.*;
import android.os.CountDownTimer;
public class TapMe extends Activity {
private int countValue=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tap_me);
checkTapValue();
}
private void checkTapValue()
{
Button tapButton=(Button)findViewById(R.id.tapButton);
tapButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
countValue++;
TextView textView;
textView = (TextView) findViewById(R.id.textView1);
textView.setTextSize(40);
textView.setText(Integer.toString(countValue));
}
});
}
@Override
protected void onResume()
{
super.onResume();
checkTapValue();
}
}
class MyCount extends CountDownTimer{
TextView tv;
public MyCount(long millisInFuture, long countDownInterval) {
super(60000, 1000);
}
@Override
public void onFinish(){}
@Override
public void onTick(long millisUntilFinished) {
tv.setText((int) (millisUntilFinished/1000));
}
}
This is my .java file. The countdown timer should display 60 seconds and count down per second. Thing is everything works fine but the countdown timer. It doesn’t display.
Can anyone point out what I did wrong, please?
Also, here’s the layout file.
<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"
android:background="#FFFFFF" >
<Button
android:id="@+id/tapButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="186dp"
android:padding="15dp"
android:text="@string/tap_me"
android:textSize="32dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_alignParentTop="true"
android:layout_marginTop="85dp"
android:padding="@dimen/padding_medium"
android:textColor="#000000"
tools:context=".TapMe" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:padding="@dimen/padding_medium"
android:textColor="#000000"
tools:context=".TapMe" />
</RelativeLayout>
EDIT
@imran
I’ve done this. No difference.
class MyCount extends CountDownTimer{
TextView tv = (TextView) findViewById(R.id.textView2);
public MyCount(long millisInFuture, long countDownInterval) {
super(60000, 1000);
}
private TextView findViewById(int textview2) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onFinish(){}
@Override
public void onTick(long millisUntilFinished) {
tv.setText("" +(int) (millisUntilFinished/1000));
}
}
try this updated code. works just fine. you didn’t started the countdown timer at all and never initialized the textview in timer. i changed these errors in below code