postheadericon Android ProgressDialog Example




MainActivity.java

package com.bipin.rupadiya.pdb;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.btnShow);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
final ProgressDialog pd = new ProgressDialog(arg0.getContext());
pd.setMax(100);
pd.setProgress(0);
pd.setCancelable(true);
pd.setTitle("www.BipinRupadiya.com");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
new Thread() {
public void run() {
try {
int i = 0;
while (i <= 100) {
sleep(500);
i++;
if (i % 10 == 0) {
pd.setProgress(pd.getProgress() + 10);
}
}

} catch (Exception e) {
// TODO: handle exception
}
pd.dismiss();

};
}.start();

}
});
}

@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;
}

}


activity_main.xml

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

    <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="@string/hello_world" />

    <Button
        android:id="@+id/btnShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="96dp"
        android:text="Show" />

</RelativeLayout>


0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.