Created
December 15, 2015 03:45
-
-
Save devrath/2c89dbf3a9307f96f13b to your computer and use it in GitHub Desktop.
Showing a Custom Loader in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ------------------------------------------------------------------------------------------------------------------------------ | |
| TransparentProgressDialog.java | |
| public class TransparentProgressDialog extends Dialog { | |
| private ImageView iv; | |
| public TransparentProgressDialog(Context context, int resourceIdOfImage) { | |
| super(context, R.style.TransparentProgressDialog); | |
| WindowManager.LayoutParams wlmp = getWindow().getAttributes(); | |
| wlmp.gravity = Gravity.CENTER_HORIZONTAL; | |
| getWindow().setAttributes(wlmp); | |
| setTitle(null); | |
| setCancelable(false); | |
| setOnCancelListener(null); | |
| LinearLayout layout = new LinearLayout(context); | |
| layout.setOrientation(LinearLayout.VERTICAL); | |
| LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(75, 75); | |
| iv = new ImageView(context); | |
| iv.setImageResource(resourceIdOfImage); | |
| layout.addView(iv, params); | |
| addContentView(layout, params); | |
| } | |
| @Override | |
| public void show() { | |
| super.show(); | |
| RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f); | |
| anim.setInterpolator(new LinearInterpolator()); | |
| anim.setRepeatCount(Animation.INFINITE); | |
| anim.setDuration(3000); | |
| iv.setAnimation(anim); | |
| iv.startAnimation(anim); | |
| } | |
| } | |
| ------------------------------------------------------------------------------------------------------------------------------ | |
| styles.xml | |
| <!-- Transparent dialog --> | |
| <style name="TransparentProgressDialog" parent="android:style/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">true</item> | |
| <item name="android:background">@android:color/transparent</item> | |
| </style> | |
| ------------------------------------------------------------------------------------------------------------------------------ | |
| private class LongOperation extends AsyncTask<String, Void, String> { | |
| TransparentProgressDialog pd; | |
| @Override | |
| protected String doInBackground(String... params) { | |
| return "Executed"; | |
| } | |
| @Override | |
| protected void onPostExecute(String result) { | |
| if (pd.isShowing()) | |
| pd.dismiss(); | |
| } | |
| @Override | |
| protected void onPreExecute() { | |
| pd = new TransparentProgressDialog(getActivity(), R.drawable.loader); | |
| } | |
| @Override | |
| protected void onProgressUpdate(Void... values) {} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment