user1769501
user1769501

Reputation:

Showing circular Progressbar Against Button Click Using Timer Class

i have coded simple calculator with two input boxes and once total button , What i have to is show a circular Progressbar for 3 seconds before i show result after pressing Total button ,here is what i have done .

package com.example.calculator;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity 
   {
     EditText t1;
     EditText t2;
     TextView tv;
     TextView tv1; 
     Timer singleTask;
     int Interval = 3000; 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t1= (EditText)findViewById(R.id.editText1);
        t2= (EditText)findViewById(R.id.editText2);
        tv=  (TextView)findViewById(R.id.textView1);
        tv1= (TextView)findViewById(R.id.textView2);
        singleTask= new Timer();

    }
   public void loadprogressbar()
    {
          singleTask.schedule(new TimerTask() {
                @Override
                public void run() {

                // have to add code for progress bar but right now just caption text added 
                    tv1.setText("This is for 3 seconds only"); 

                }
                }, 1000);

    }

   @Override
   protected void onDestroy(){
   super.onDestroy();
   if(singleTask != null)
   {
   singleTask.cancel();
   }
   }
   public void   Clicked(View v)
    {    int  total; 


         if(v.getId()==R.id.button1)
         {     
             int v1 = Integer.parseInt(t1.getText().toString());
             int v2 = Integer.parseInt(t2.getText().toString());
             total = v1 + v2;
             loadprogressbar();
             tv.setText(total+"");
             tv.setVisibility(1);
         }
         else if (v.getId()==R.id.button2)
         {
             t1.setText("");
             t2.setText("");
         }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Xml File is

  <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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:visibility="invisible"/>
     <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        ></TextView>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="21dp"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignRight="@+id/editText2"
        android:text="Clear"
        android:onClick="Clicked" 
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="35dp"
        android:text="Total"
        android:onClick="Clicked"
         />

</RelativeLayout>

Problem is that i am not getting any result against Timer code ? according to my thinking it should show text for 3 seconds and then display text . My logic could be wrong as well please guide me ..

Upvotes: 0

Views: 1263

Answers (3)

EMarci15
EMarci15

Reputation: 303

So I think your code is wrong in loadprogressbar(). It should be:

public void loadprogressbar()
{
      // have to add code for progress bar but right now just caption text added 
      tv1.setText("This is for 3 seconds only"); 
      singleTask.schedule(new TimerTask() {
            @Override
            public void run() {

            runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                                 ///hide the progressbar here...
                              }
                            });

            }
            }, Interval);

}

And if I were you I would change the name of your function "Clicked", because it can be a reserved word (the color also indicates it)...

Upvotes: 0

Andy Res
Andy Res

Reputation: 16043

Go with E.Odebugg answer.

If you however need to display a TextView for 3 seconds and then hide, here's how to do it without using TimerTask.

textView.postDelayed(new Runnable() {
    @Override
    public void run() {
        textView.setVisibility(View.GONE);
    }
}, 3000);

The TextView is displayed by default, and after 3 seconds it's hidden. Also, note that postDelayed() is used, which will run the Runnable on UI thread.

Upvotes: 1

Emmanuel
Emmanuel

Reputation: 13223

If I understand correctly what you want to do is to show a ProgressDialog to simulate that your calculator is doing some calculation. The Timer class is not what you want to use. Basically, you want to wait for 3 seconds (during that time the ProgressDialog will be visible), and then update the UI by setting the TextView with the result. As you might know, you cannot modify the UI thread from a background Thread directly, and you should not make the UI thread sleep. Hence, AsyncTask is your best bet. Below is a sample code to allow you to show a ProgressDialog for 3 seconds. You should update your TextView on the OnPostExecute method since it runs on the UI thread.

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity{
ProgressDialog pd;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      pd = new ProgressDialog(this);
    pd.setTitle(getString("Title"));
    pd.setIndeterminate(true);
   pd.show();
    new MyAsycnTask().execute();

    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private class MyAsycnTask extends AsyncTask <Void,Void,Void> {


    @Override
    protected Void doInBackground(Void... voids) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;

    }


    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        pd.hide();




    }
}
}

Upvotes: 2

Related Questions