I want to display numbers in a TextView like this:
Wait 5 sec // then a delay of 1 sec
Wait 4 sec // display this in the same text view along with delay
Wait 3 sec // display this in the same text view along with delay
Wait 2 sec // display this in the same text view along with delay
Wait 1 sec // display this in the same text view along with delay
I want perform this action on a button click and it should work like a count down timer.
You can use this one
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("Wait: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
This timer class can start at the time of button click and also you can adjust the total time and the tick interval
This is from developer site
If I understand you correctly, you just want a countdown timer starting at 5 seconds on the click of a button. If so, give this a shot:
public class MainActivity extends Activity
{
TextView textView;
Button button;
private final Handler mHandler = new Handler();
private int startTime = 5;
private Runnable mTask = new Runnable()
{
public void run()
{
if (startTime > 0)
{
textView.setText("Wait for " + String.valueOf(startTime));
startTime--;
mHandler.postDelayed(mTask, 1000);
}
else
{
textView.setText("Sorry. Times up!");
}
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mHandler.post(mTask);
}
}
);
}
}
Its very easy, You need to user CountDownTimer class.
Here is a working demo, I just created for you.
MainActivity.java
public class MainActivity extends Activity
{
int remaningTime = 5;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById( R.id.txtTextView );
Button button1 = (Button) findViewById( R.id.button1 );
button1.setOnClickListener( new OnClickListener ()
{
@Override
public void onClick(View arg0)
{
new CountDownTimer ( remaningTime * 1000, 1000 )
{
@Override
public void onFinish()
{
}
@Override
public void onTick(long millisUntilFinished)
{
textView.setText( "Wait " + ( remaningTime-- ) + " sec" );
}
}.start();
}
});
}
}
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:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/txtTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtTextView"
android:layout_below="@+id/txtTextView"
android:layout_marginTop="34dp"
android:text="Button" />
</RelativeLayout>
Use an AsyncTask, here is some sample code:
...
private class CounterTask extends AsyncTask<null, null, null> {
@Override
protected void onPreExecute() {
}
@Override
protected Long doInBackground(Uri... uri) {
try {
mCount++;
updateUIHandler.obtainMessage().sendToTarget();
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Long result) {
}
}
public Handler updateUIHandler = new Handler() {
public void handleMessage(Message msg) {
mText.setText(mCount);
}
};
mText
is your TextView and mCount
is your counter, these can be global variables. As iTech has stated you can't update UI from a thread, however you can make a call to a Handler on the UI thread to do the update.
And run the task like this:
new CounterTask().execute(null, null, null);