Want To Display Number With 1sec Delay
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
Solution 1:
You can use this one
new CountDownTimer(30000, 1000) {
publicvoidonTick(long millisUntilFinished) {
mTextField.setText("Wait: " + millisUntilFinished / 1000);
}
publicvoidonFinish() {
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
Solution 2:
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:
publicclassMainActivityextendsActivity
{
TextView textView;
Button button;
privatefinalHandlermHandler=newHandler();
privateintstartTime=5;
privateRunnablemTask=newRunnable()
{
publicvoidrun()
{
if (startTime > 0)
{
textView.setText("Wait for " + String.valueOf(startTime));
startTime--;
mHandler.postDelayed(mTask, 1000);
}
else
{
textView.setText("Sorry. Times up!");
}
}
};
@OverridepublicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(newOnClickListener()
{
@OverridepublicvoidonClick(View v)
{
mHandler.post(mTask);
}
}
);
}
}
Solution 3:
Its very easy, You need to user CountDownTimer class.
Here is a working demo, I just created for you.
MainActivity.java
publicclassMainActivityextendsActivity
{
intremaningTime=5;
TextView textView;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById( R.id.txtTextView );
Buttonbutton1= (Button) findViewById( R.id.button1 );
button1.setOnClickListener( newOnClickListener ()
{
@OverridepublicvoidonClick(View arg0)
{
newCountDownTimer ( remaningTime * 1000, 1000 )
{
@OverridepublicvoidonFinish()
{
}
@OverridepublicvoidonTick(long millisUntilFinished)
{
textView.setText( "Wait " + ( remaningTime-- ) + " sec" );
}
}.start();
}
});
}
}
main.xml
<RelativeLayoutxmlns: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" ><TextViewandroid: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" /><Buttonandroid: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>
Solution 4:
Use an AsyncTask, here is some sample code:
...
privateclassCounterTaskextendsAsyncTask<null, null, null> {
@OverrideprotectedvoidonPreExecute() {
}
@OverrideprotectedLongdoInBackground(Uri... uri) {
try {
mCount++;
updateUIHandler.obtainMessage().sendToTarget();
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
returnnull;
}
@OverrideprotectedvoidonPostExecute(Long result) {
}
}
publicHandler updateUIHandler = newHandler() {
publicvoidhandleMessage(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);
Post a Comment for "Want To Display Number With 1sec Delay"