Creating Prograss Bar in Android
-------------------------------------
Activity File
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ProgBar" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/prog_msg" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="@string/msg"
/>
</LinearLayout>
Java Code
-------------
package com.progbar;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ProgBar extends Activity {
ProgressDialog pb;
int value=0;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progbar_activity);
Button b=(Button) findViewById(R.id.b1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(1);
pb.setProgress(0);
handler.sendEmptyMessage(0);
}
});
handler=new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
if(value>=100)
{
pb.dismiss();
}
else
{
value++;
pb.incrementProgressBy(1);
handler.sendEmptyMessageDelayed(0,100);
}
}
};
}
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case 0:
return new AlertDialog.Builder(this).create();
case 1:
pb=new ProgressDialog(this);
pb.setIcon(R.drawable.ic_launcher);
pb.setTitle("Download files...");
pb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pb.setButton(DialogInterface.BUTTON_POSITIVE,"Hide",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int whichButton)
{
Toast.makeText(getBaseContext(), "Hide Clicked !",Toast.LENGTH_LONG).show();
}
});
return pb;
}
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.progbar_activity, menu);
return true;
}
}
No comments:
Post a Comment