Saturday 26 January 2013

ProgressDialog in Android Example With Thread Concept

This is Program is used for seperate thread , which is execute ProgressDialog Box........


program1 : MainActivity.java
package com.example.progressdialogdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

    private ProgressDialog dialog ;
    private Handler myhandler ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        showProgressbar();
        new Thread(new Runnable() {
           
            @Override
            public void run() {

                SecondClass obj = new SecondClass(myhandler,dialog);
                obj.doWork();
               
            }
        }).start();
       
       
       
    }

    private void showProgressbar() {
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER) ;
        dialog.setTitle("Data Sending To Server");
        dialog.setMessage("Loading..Wait..Till It Send..");
        //dialog.show();
        dialog.setCancelable(false);
        dialog.show();
       
    }

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

}
program 2 : SecondClass.java
package com.example.progressdialogdemo;

import android.app.ProgressDialog;
import android.os.Handler;

public class SecondClass {

    private ProgressDialog dialog ;
    private Handler hand ;
   
   

    public SecondClass(Handler myhandler, ProgressDialog dialog2) {
       
        hand = myhandler ;
        dialog = dialog2 ;
    }



    public void doWork() {

           
            dialog.show();
           
       
    }

}

xml part :
<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" >

</RelativeLayout>

AsyncTask in android Example simple Program


AsyncTask Example Program :

AsyncTaskActivity.java

package com.example.asynctaskexample;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class AsyncTaskActivity extends Activity {

    Button btn_Start ;
    ProgressBar progressbar ;
    TextView text_percentage ;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async_task);
       
      btn_Start = (Button) findViewById(R.id.btn_start) ;
      progressbar = (ProgressBar) findViewById(R.id.progress) ;
      text_percentage = (TextView) findViewById(R.id.txt_percentage) ;

     
      btn_Start.setOnClickListener(new OnClickListener() {
       
        @Override
        public void onClick(View v) {
           
            btn_Start.setEnabled(false) ;
            new ShowDialogAsyncTask().execute() ;
           
        }
    });
     
    }

   private class ShowDialogAsyncTask extends AsyncTask< Void , Integer , Void > {

       int progress_status ;
      
       protected void onPreExecute() {
           Log.v("hari","onPreExecute-------------") ;
           // update the UI immediately after the task is executed
           super.onPreExecute();
           
            Toast.makeText(AsyncTaskActivity.this,"Invoke onPreExecute()", Toast.LENGTH_SHORT).show();
       
            progress_status = 0;
            text_percentage.setText("downloading 0%");
           
          }
    @Override
    protected Void doInBackground(Void... params) {
       
        Log.v("hari","doInBackground-----------:params:"+params) ;
       
        while( progress_status < 100 ) {Log.v("hari","doInBackground-------while") ;
           
            progress_status += 2 ;
            publishProgress(progress_status);
            SystemClock.sleep(300);
        }
        return null;
    }
   
    protected void onProgressUpdate(Integer... values) {
       
           super.onProgressUpdate(values);
           Log.v("hari","onProgressUpdate-----------values:-"+values) ;
           progressbar.setProgress(values[0]);
           text_percentage.setText("downloading " +values[0]+"%");
           
          }
   
    protected void onPostExecute(Void result) {
           super.onPostExecute(result);
           Log.v("hari","onPostExecute------------") ;
            Toast.makeText(AsyncTaskActivity.this,"Invoke onPostExecute()", Toast.LENGTH_SHORT).show();
            
            text_percentage.setText("download complete");
            btn_Start.setEnabled(true);
          }
   }
   
}

UI :
activity_async_task.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
       
        android:text="Android Async Task"
         />

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="34dp" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progress"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:minWidth="120dp"
        android:text="start_btn" />

    <TextView
        android:id="@+id/txt_percentage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/progress"
        android:text="downloading  0%"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

Complete Details about eLitmus pH Test at Rs:699/- Share your java material and fresher interview Information for us to Help Others... mail to : vhkrishnan.v@gmail.com