Last active
May 19, 2022 06:59
-
-
Save esabook/b0e7f93922c595d23273d52c4b791da1 to your computer and use it in GitHub Desktop.
Fullscreen dialog with dialogFragment, support image, header, title, message, custom layout for message, button
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
| package ***; | |
| import android.content.res.Resources; | |
| import android.graphics.drawable.Drawable; | |
| import android.os.Bundle; | |
| import android.support.annotation.DrawableRes; | |
| import android.support.annotation.LayoutRes; | |
| import android.support.annotation.NonNull; | |
| import android.support.annotation.Nullable; | |
| import android.support.v4.content.res.ResourcesCompat; | |
| import android.support.v7.app.AppCompatDialogFragment; | |
| import android.support.v7.widget.AppCompatImageView; | |
| import android.support.v7.widget.AppCompatTextView; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.LinearLayout; | |
| import android.widget.Toast; | |
| /** | |
| * example usage: | |
| * <code> | |
| * final DialogMessageFragment dialog = new DialogMessageFragment() | |
| * .setMessage(getString(R.string.message)) | |
| * .setHeader(getString(R.string.error_token)) | |
| * .setBotton("Lanjutkan") | |
| * .setImage(ResourcesCompat.getDrawable(getResources(), R.drawable.alert_box_success, null)) | |
| * .setOverlayMode(true) | |
| * .setButtonBorderless(true); | |
| * | |
| * dialog.showNow(getSupportFragmentManager(), null); | |
| * | |
| * dialog.getButton().setOnClickListener(new View.OnClickListener() { | |
| * | |
| * @Override public void onClick(View v) { | |
| * Toast.makeText(v.getContext(), "click", Toast.LENGTH_SHORT).show(); | |
| * dialog.dismiss(); | |
| * } | |
| * }); | |
| * </code> | |
| */ | |
| public class DialogMessageFragment extends AppCompatDialogFragment implements View.OnClickListener { | |
| private Drawable mImageDrawable; | |
| private String mHeader; | |
| private String mMessage; | |
| private String mButton; | |
| @LayoutRes | |
| private int mCustomMessageId = 0; | |
| private boolean isUseCustomMessage; | |
| private boolean mButtonStyleBorderless; | |
| private boolean mOverlayMode = true; | |
| private View.OnClickListener mButtonOnClickListeter; | |
| private AppCompatImageView vImage; | |
| private AppCompatTextView vHeader; | |
| private AppCompatTextView vMessage; | |
| private AppCompatTextView vButton; | |
| private AppCompatImageView vCloseButton; | |
| private LinearLayout vCustomMessageBox; | |
| private View vCustomMessage; | |
| //region GETTER-SETTER | |
| public DialogMessageFragment setImage(Drawable drawable) { | |
| this.mImageDrawable = drawable; | |
| invalidateView(); | |
| return this; | |
| } | |
| public DialogMessageFragment setImage(Resources res, @DrawableRes int id) { | |
| setImage(ResourcesCompat.getDrawable(res, id, null)); | |
| return this; | |
| } | |
| public DialogMessageFragment setMessage(String message) { | |
| this.mMessage = message; | |
| this.isUseCustomMessage = false; | |
| invalidateView(); | |
| return this; | |
| } | |
| public DialogMessageFragment setViewAsMessage(@LayoutRes int customMessage) { | |
| this.mCustomMessageId = customMessage; | |
| this.isUseCustomMessage = true; | |
| this.vCustomMessage = null; | |
| invalidateView(); | |
| return this; | |
| } | |
| public DialogMessageFragment setHeader(String header) { | |
| this.mHeader = header; | |
| invalidateView(); | |
| return this; | |
| } | |
| public DialogMessageFragment setBotton(String button) { | |
| this.mButton = button; | |
| invalidateView(); | |
| return this; | |
| } | |
| public DialogMessageFragment setOverlayMode(boolean mode) { | |
| this.mOverlayMode = mode; | |
| invalidateView(); | |
| return this; | |
| } | |
| public DialogMessageFragment setButtonBorderless(boolean style) { | |
| this.mButtonStyleBorderless = style; | |
| invalidateView(); | |
| return this; | |
| } | |
| public DialogMessageFragment setOnButtonClickListener(View.OnClickListener o) { | |
| this.mButtonOnClickListeter = o; | |
| invalidateView(); | |
| return this; | |
| } | |
| //endregion | |
| void invalidateView() { | |
| if (vImage != null) { | |
| vImage.setImageDrawable(mImageDrawable); | |
| vImage.setVisibility(mImageDrawable == null ? View.GONE : View.VISIBLE); | |
| } | |
| if (vHeader != null) vHeader.setText(mHeader); | |
| if (vMessage != null) { | |
| vMessage.setText(mMessage); | |
| vMessage.setVisibility(isUseCustomMessage ? View.GONE : View.VISIBLE); | |
| } | |
| if (vButton != null) { | |
| vButton.setText(mButton); | |
| vButton.setOnClickListener(mButtonOnClickListeter == null | |
| ? this | |
| : mButtonOnClickListeter); | |
| // if (mButtonStyleBorderless){ | |
| // vButton.setTextAppearance(getContext(),R.style.AppTheme_Button_BorderLess); | |
| // }else { | |
| // vButton.setTextAppearance(getContext(),R.style.AppTheme_Button); | |
| // } | |
| } | |
| if (vCustomMessageBox != null) { | |
| if (isUseCustomMessage) { | |
| try { | |
| if (vCustomMessage == null) | |
| vCustomMessage = LayoutInflater.from(vCustomMessageBox.getContext()) | |
| .inflate(mCustomMessageId, vCustomMessageBox, false); | |
| } catch (Exception ignore) { | |
| } | |
| if (vCustomMessage != null) { | |
| vCustomMessageBox.addView(vCustomMessage); | |
| } | |
| } else { | |
| vCustomMessageBox.removeAllViews(); | |
| } | |
| } | |
| } | |
| //region OVERIDED LIFECYCLE | |
| @Override | |
| public int getTheme() { | |
| return R.style.AppTheme_PopupAlertDialog; | |
| } | |
| @Nullable | |
| @Override | |
| public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
| View v = inflater.inflate(R.layout.dialog_message, container, false); | |
| v.findViewById(R.id.rootLayout).setBackgroundResource(mOverlayMode ? 0 : R.color.green); | |
| vImage = v.findViewById(R.id.iv_icon); | |
| vHeader = v.findViewById(R.id.tv_header); | |
| vMessage = v.findViewById(R.id.tv_message); | |
| vCustomMessageBox = v.findViewById(R.id.custom_message); | |
| vButton = v.findViewById(R.id.button); | |
| vCloseButton = v.findViewById(R.id.close); | |
| vCloseButton.setVisibility(isCancelable() ? View.VISIBLE : View.GONE); | |
| vCloseButton.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| DialogMessageFragment.this.dismiss(); | |
| } | |
| }); | |
| vCloseButton.setOnLongClickListener(new View.OnLongClickListener() { | |
| @Override | |
| public boolean onLongClick(View v) { | |
| Toast.makeText(getActivity(), "Close", Toast.LENGTH_SHORT).show(); | |
| return true; | |
| } | |
| }); | |
| return v; | |
| } | |
| @Override | |
| public void onResume() { | |
| super.onResume(); | |
| invalidateView(); | |
| } | |
| @Override | |
| public void onClick(View v) { | |
| dismiss(); | |
| } | |
| //endregion | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment