Skip to content

Instantly share code, notes, and snippets.

@himanshuvirmani
Created April 11, 2014 11:01
Show Gist options
  • Save himanshuvirmani/10458548 to your computer and use it in GitHub Desktop.
Save himanshuvirmani/10458548 to your computer and use it in GitHub Desktop.
Creating a full screen progress bar component for Android
public final class CustomProgressBar {
private Dialog dialog;
public Dialog show(Context context) {
return show(context, null);
}
public Dialog show(Context context, CharSequence title) {
return show(context, title, false);
}
public Dialog show(Context context, CharSequence title, boolean cancelable) {
return show(context, title, cancelable, null);
}
public Dialog show(Context context, CharSequence title, boolean cancelable,
OnCancelListener cancelListener) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflator.inflate(R.layout.progress_bar, null);
if(title != null) {
final TextView tv = (TextView) view.findViewById(R.id.id_title);
tv.setText(title);
}
dialog = new Dialog(context, R.style.NewDialog);
dialog.setContentView(view);
dialog.setCancelable(cancelable);
dialog.setOnCancelListener(cancelListener);
dialog.show();
return dialog;
}
public Dialog getDialog() {
return dialog;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ProgressBar
android:id="@+id/id_pbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/id_title"
android:layout_below="@+id/id_pbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/please_wait"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
<resources>
<style name="NewDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">@android:color/transparent</item>
</style>
</resources>
// Make this static as there will be just one instance per activity
private static CustomProgressBar progressBar = new CustomProgressBar();
// Show progress bar
progressBar.show(this,"Loading..");
// Dismiss progress bar
progressBar.getDialog().dismiss();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment