main.xml
<LinearLayout 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:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Progress Bar"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textColor="#336666"
android:textSize="15dp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="Download File" />
</LinearLayout>
ProgressBarActivity.java
package com.rakesh.tiwari.progressbar;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ProgressBarActivity extends Activity {
Button button;
private int progress=0;
private ProgressDialog progressDialog;
private Handler progressHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(1);
progress=0;
progressDialog.setProgress(0);
progressHandler.sendEmptyMessage(0);
}
});
progressHandler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
if(progress>=100){
progressDialog.dismiss();
}
else{
progress++;
progressDialog.incrementProgressBy(1);
progressHandler.sendEmptyMessageDelayed(0,100);
}
}
};
}
protected Dialog onCreateDialog(int i){
progressDialog=new ProgressDialog(this);
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setTitle("Downloading files.....");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Hide",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hide Clicked", Toast.LENGTH_LONG).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Cancel Clicked", Toast.LENGTH_LONG).show();
}
});
return progressDialog;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rakesh.tiwari.progressbar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:screenOrientation="portrait"
android:name="com.rakesh.tiwari.progressbar.ProgressBarActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<LinearLayout 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:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Progress Bar"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textColor="#336666"
android:textSize="15dp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="Download File" />
</LinearLayout>
ProgressBarActivity.java
package com.rakesh.tiwari.progressbar;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ProgressBarActivity extends Activity {
Button button;
private int progress=0;
private ProgressDialog progressDialog;
private Handler progressHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(1);
progress=0;
progressDialog.setProgress(0);
progressHandler.sendEmptyMessage(0);
}
});
progressHandler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
if(progress>=100){
progressDialog.dismiss();
}
else{
progress++;
progressDialog.incrementProgressBy(1);
progressHandler.sendEmptyMessageDelayed(0,100);
}
}
};
}
protected Dialog onCreateDialog(int i){
progressDialog=new ProgressDialog(this);
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setTitle("Downloading files.....");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Hide",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hide Clicked", Toast.LENGTH_LONG).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Cancel Clicked", Toast.LENGTH_LONG).show();
}
});
return progressDialog;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rakesh.tiwari.progressbar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:screenOrientation="portrait"
android:name="com.rakesh.tiwari.progressbar.ProgressBarActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment