/** * Sets an OnToastButtonClickListener to the button in a a BUTTON type SuperCardToast. <br> * IMPORTANT: On Devices > API 11 any listener passed will automaticly be saved to a retained * Fragment for preservation on orientation changes. <br> * * @param onToastButtonClickListener Use OnToastButtonClickListenerHolder for orientation change * support */ public void setOnToastButtonClickListener( OnToastButtonClickListenerHolder onToastButtonClickListener) { mToastButton.setOnClickListener(onToastButtonClickListener); this.mClickListenerTag = onToastButtonClickListener.getTag(); /** On devices > API 11 save listener to retained Fragment */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { final Activity activity = (Activity) mContext; FragmentManager fragmentManager = activity.getFragmentManager(); FragmentRetainer fragmentRetainer = (FragmentRetainer) fragmentManager.findFragmentByTag(FRAGMENTRETAINER_ID); if (fragmentRetainer == null) { fragmentRetainer = new FragmentRetainer(); fragmentManager.beginTransaction().add(fragmentRetainer, FRAGMENTRETAINER_ID).commit(); } fragmentRetainer.addClickListenerToList(onToastButtonClickListener); } }
/** Method used to recreate SuperCardToasts after orientation change */ private SuperCardToast( Activity activity, Style style, List<OnToastButtonClickListenerHolder> onToastButtonClickListeners, List<OnToastDismissListenerHolder> onToastDismissListeners, int position) { SuperCardToast superCardToast; if (style.mType == Type.BUTTON) { superCardToast = new SuperCardToast(activity, Type.BUTTON); superCardToast.setButtonText(style.mButtonText); superCardToast.setButtonTextSizeFloat(style.mButtonTextSize); superCardToast.setButtonTextColor(style.mButtonTextColor); superCardToast.setButtonResource(style.mButtonResource); superCardToast.setButtonDividerResource(style.mButtonDividerResource); superCardToast.setButtonTypeface(style.mButtonTypeface); /** Reattach any OnToastButtonClickListeners via retained Fragment */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { FragmentRetainer fragmentRetainer = (FragmentRetainer) activity.getFragmentManager().findFragmentByTag(FRAGMENTRETAINER_ID); if (fragmentRetainer != null && fragmentRetainer.getClickListeners() != null) { for (OnToastButtonClickListenerHolder onToastButtonClickListenerHolder : fragmentRetainer.getClickListeners()) { if (onToastButtonClickListenerHolder.getTag().equals(style.mClickListenerTag)) { superCardToast.setOnToastButtonClickListener(onToastButtonClickListenerHolder); } } } /** Reattach any OnToastButtonClickListeners via provided List */ } else { if (onToastButtonClickListeners != null) { for (OnToastButtonClickListenerHolder onToastButtonClickListenerHolder : onToastButtonClickListeners) { if (onToastButtonClickListenerHolder .getTag() .equalsIgnoreCase(style.mClickListenerTag)) { superCardToast.setOnToastButtonClickListener(onToastButtonClickListenerHolder); } } } } } else if (style.mType == Type.PROGRESS) { /** PROGRESS style SuperCardToasts should be managed by the developer */ return; } else if (style.mType == Type.PROGRESS_HORIZONTAL) { /** PROGRESS_HORIZONTAL style SuperCardToasts should be managed by the developer */ return; } else { superCardToast = new SuperCardToast(activity); } /** Reattach any OnToastBDismissListeners via retained Fragment */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { FragmentRetainer fragmentRetainer = (FragmentRetainer) activity.getFragmentManager().findFragmentByTag(FRAGMENTRETAINER_ID); if (fragmentRetainer != null && fragmentRetainer.getDismissListeners() != null) { for (OnToastDismissListenerHolder onToastDismissListenerHolder : fragmentRetainer.getDismissListeners()) { if (onToastDismissListenerHolder.getTag().equals(style.mDismissListenerTag)) { superCardToast.setOnToastDismissListener(onToastDismissListenerHolder); } } } /** Reattach any OnToastBDismissListeners via provided List */ } else { if (onToastDismissListeners != null) { for (OnToastDismissListenerHolder onToastDismissListenerHolder : onToastDismissListeners) { if (onToastDismissListenerHolder.getTag().equalsIgnoreCase(style.mDismissListenerTag)) { superCardToast.setOnToastDismissListener(onToastDismissListenerHolder); } } } } superCardToast.setAnimations(style.mAnimations); superCardToast.setText(style.mText); superCardToast.setTypeface(style.mTypeface); superCardToast.setDuration(style.mDuration); superCardToast.setTextColor(style.mTextColor); superCardToast.setTextSizeFloat(style.mTextSize); superCardToast.setIndeterminate(style.isIndeterminate); superCardToast.setIcon(style.mIconResource, style.mIconPosition); superCardToast.setBackgroundResource(style.mBackgroundResource); /** Must use if else statements here to prevent erratic behavior */ if (style.isTouchDismissable) { superCardToast.setTouchToDismiss(true); } else if (style.isSwipeDismissable) { superCardToast.setSwipeToDismiss(true); } superCardToast.setShowImmediate(true); superCardToast.show(); }