0

I am beginner Android programmer. I am coding a small calculator where two no's are entered and on pressing total both are added . On pressing Total there must be a intermediate progress bar(circular ) just for 3 seconds and then there appears total value ,I have done calculator and progress bar but dont know how to put them togather in asynctask , calc code is here . please guide me how to do it

  <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>

Total is perfomed here

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("");
     }
}

i have code for ProgressBar as well but really getting no idea how to use both these codes together with asyncTask to load Progressbar first and then toatl value

Elletlar
  • 3,136
  • 7
  • 32
  • 38

2 Answers2

1

Basically you need to do the following things:

  1. Show the progress bar.
  2. Wait for some time.
  3. Show the results.

The second item should be done in some other thread than the main because otherwise the UI will hang. The other tasks, however, must be done on the main thread because they manipulate the UI. You don't really need to use AsyncTask for such a simple case, you should use a Handler instead:

//Display the progress bar here

//Create a handler and tell it to run a task 3 seconds later
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        //Hide the progress bar and show the results
    }
}, 3 * 1000); //3 second delay is specified here
Malcolm
  • 41,014
  • 11
  • 68
  • 91
0

Add the ProgressBar to your layout, but set its visibility to "gone" (android:visibility="gone") Just before you call AsyncTask.execute(), set the ProgressBar to visible (View.setVisibility(View.VISIBLE). Then in onPostExecute(), set it to "gone" again (View.setVisibility(View.GONE).

Joe Malin
  • 8,621
  • 1
  • 23
  • 18