Android Open Source - android-styled-dialogs Simple Dialog Fragment






From Project

Back to project page android-styled-dialogs.

License

The source code is released under:

Apache License

If you think the Android project android-styled-dialogs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2013 Inmite s.r.o. (www.inmite.eu).
 */* ww w  .j a va2s.co  m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.avast.android.dialogs.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.Html;
import android.text.SpannedString;
import android.text.TextUtils;
import android.view.View;

import com.avast.android.dialogs.core.BaseDialogBuilder;
import com.avast.android.dialogs.core.BaseDialogFragment;
import com.avast.android.dialogs.iface.ISimpleDialogCancelListener;
import com.avast.android.dialogs.iface.ISimpleDialogListener;

/**
 * Dialog for displaying simple message, message with title or message with title and two buttons. Implement {@link
 * com.avast.android.dialogs.iface.ISimpleDialogListener} in your Fragment or Activity to rect on positive and negative button clicks. This class can
 * be extended and more parameters can be added in overridden build() method.
 *
 * @author David Vvra (david@inmite.eu)
 */
public class SimpleDialogFragment extends BaseDialogFragment {

  protected final static String ARG_MESSAGE = "message";
  protected final static String ARG_TITLE = "title";
  protected final static String ARG_POSITIVE_BUTTON = "positive_button";
  protected final static String ARG_NEGATIVE_BUTTON = "negative_button";
  protected final static String ARG_NEUTRAL_BUTTON = "neutral_button";

  protected int mRequestCode;

  public static SimpleDialogBuilder createBuilder(Context context, FragmentManager fragmentManager) {
    return new SimpleDialogBuilder(context, fragmentManager, SimpleDialogFragment.class);
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final Fragment targetFragment = getTargetFragment();
    if (targetFragment != null) {
      mRequestCode = getTargetRequestCode();
    } else {
      Bundle args = getArguments();
      if (args != null) {
        mRequestCode = args.getInt(BaseDialogBuilder.ARG_REQUEST_CODE, 0);
      }
    }
  }

  /**
   * Children can extend this to add more things to base builder.
   */
  @Override
  protected BaseDialogFragment.Builder build(BaseDialogFragment.Builder builder) {
    final String title = getTitle();
    if (!TextUtils.isEmpty(title)) {
      builder.setTitle(title);
    }

    final CharSequence message = getMessage();
    if (!TextUtils.isEmpty(message)) {
      builder.setMessage(message);
    }

    final String positiveButtonText = getPositiveButtonText();
    if (!TextUtils.isEmpty(positiveButtonText)) {
      builder.setPositiveButton(positiveButtonText, new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          ISimpleDialogListener listener = getDialogListener();
          if (listener != null) {
            listener.onPositiveButtonClicked(mRequestCode);
          }
          dismiss();
        }
      });
    }

    final String negativeButtonText = getNegativeButtonText();
    if (!TextUtils.isEmpty(negativeButtonText)) {
      builder.setNegativeButton(negativeButtonText, new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          ISimpleDialogListener listener = getDialogListener();
          if (listener != null) {
            listener.onNegativeButtonClicked(mRequestCode);
          }
          dismiss();
        }
      });
    }

    final String neutralButtonText = getNeutralButtonText();
    if (!TextUtils.isEmpty(neutralButtonText)) {
      builder.setNeutralButton(neutralButtonText, new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          ISimpleDialogListener listener = getDialogListener();
          if (listener != null) {
            listener.onNeutralButtonClicked(mRequestCode);
          }
          dismiss();
        }
      });
    }

    return builder;
  }

  protected CharSequence getMessage() {
    return getArguments().getCharSequence(ARG_MESSAGE);
  }

  protected String getTitle() {
    return getArguments().getString(ARG_TITLE);
  }

  protected String getPositiveButtonText() {
    return getArguments().getString(ARG_POSITIVE_BUTTON);
  }

  protected String getNegativeButtonText() {
    return getArguments().getString(ARG_NEGATIVE_BUTTON);
  }

  protected String getNeutralButtonText() {
    return getArguments().getString(ARG_NEUTRAL_BUTTON);
  }

  @Override
  public void onCancel(DialogInterface dialog) {
    super.onCancel(dialog);
    ISimpleDialogCancelListener listener = getCancelListener();
    if (listener != null) {
      listener.onCancelled(mRequestCode);
    }
  }

  protected ISimpleDialogListener getDialogListener() {
    final Fragment targetFragment = getTargetFragment();
    if (targetFragment != null) {
      if (targetFragment instanceof ISimpleDialogListener) {
        return (ISimpleDialogListener) targetFragment;
      }
    } else {
      if (getActivity() instanceof ISimpleDialogListener) {
        return (ISimpleDialogListener) getActivity();
      }
    }
    return null;
  }

  protected ISimpleDialogCancelListener getCancelListener() {
    final Fragment targetFragment = getTargetFragment();
    if (targetFragment != null) {
      if (targetFragment instanceof ISimpleDialogCancelListener) {
        return (ISimpleDialogCancelListener) targetFragment;
      }
    } else {
      if (getActivity() instanceof ISimpleDialogCancelListener) {
        return (ISimpleDialogCancelListener) getActivity();
      }
    }
    return null;
  }

  public static class SimpleDialogBuilder extends BaseDialogBuilder<SimpleDialogBuilder> {

    private String mTitle;
    private CharSequence mMessage;
    private String mPositiveButtonText;
    private String mNegativeButtonText;
    private String mNeutralButtonText;

    protected SimpleDialogBuilder(Context context, FragmentManager fragmentManager, Class<? extends SimpleDialogFragment> clazz) {
      super(context, fragmentManager, clazz);
    }

    @Override
    protected SimpleDialogBuilder self() {
      return this;
    }

    public SimpleDialogBuilder setTitle(int titleResourceId) {
      mTitle = mContext.getString(titleResourceId);
      return this;
    }


    public SimpleDialogBuilder setTitle(String title) {
      mTitle = title;
      return this;
    }

    public SimpleDialogBuilder setMessage(int messageResourceId) {
      mMessage = mContext.getText(messageResourceId);
      return this;
    }

    /**
     * Allow to set resource string with HTML formatting and bind %s,%i.
     * This is workaround for https://code.google.com/p/android/issues/detail?id=2923
     */
    public SimpleDialogBuilder setMessage(int resourceId, Object... formatArgs){
      mMessage = Html.fromHtml(String.format(Html.toHtml(new SpannedString(mContext.getText(resourceId))), formatArgs));
      return this;
    }

    public SimpleDialogBuilder setMessage(CharSequence message) {
      mMessage = message;
      return this;
    }

    public SimpleDialogBuilder setPositiveButtonText(int textResourceId) {
      mPositiveButtonText = mContext.getString(textResourceId);
      return this;
    }

    public SimpleDialogBuilder setPositiveButtonText(String text) {
      mPositiveButtonText = text;
      return this;
    }

    public SimpleDialogBuilder setNegativeButtonText(int textResourceId) {
      mNegativeButtonText = mContext.getString(textResourceId);
      return this;
    }

    public SimpleDialogBuilder setNegativeButtonText(String text) {
      mNegativeButtonText = text;
      return this;
    }

    public SimpleDialogBuilder setNeutralButtonText(int textResourceId) {
      mNeutralButtonText = mContext.getString(textResourceId);
      return this;
    }

    public SimpleDialogBuilder setNeutralButtonText(String text) {
      mNeutralButtonText = text;
      return this;
    }

    @Override
    protected Bundle prepareArguments() {
      Bundle args = new Bundle();
      args.putCharSequence(SimpleDialogFragment.ARG_MESSAGE, mMessage);
      args.putString(SimpleDialogFragment.ARG_TITLE, mTitle);
      args.putString(SimpleDialogFragment.ARG_POSITIVE_BUTTON, mPositiveButtonText);
      args.putString(SimpleDialogFragment.ARG_NEGATIVE_BUTTON, mNegativeButtonText);
      args.putString(SimpleDialogFragment.ARG_NEUTRAL_BUTTON, mNeutralButtonText);

      return args;
    }
  }
}




Java Source Code List

com.avast.android.dialogs.core.BaseDialogBuilder.java
com.avast.android.dialogs.core.BaseDialogFragment.java
com.avast.android.dialogs.fragment.DatePickerDialogFragment.java
com.avast.android.dialogs.fragment.ListDialogFragment.java
com.avast.android.dialogs.fragment.ProgressDialogFragment.java
com.avast.android.dialogs.fragment.SimpleDialogFragment.java
com.avast.android.dialogs.fragment.TimePickerDialogFragment.java
com.avast.android.dialogs.iface.IDateDialogCancelListener.java
com.avast.android.dialogs.iface.IDateDialogListener.java
com.avast.android.dialogs.iface.IListDialogListener.java
com.avast.android.dialogs.iface.ISimpleDialogCancelListener.java
com.avast.android.dialogs.iface.ISimpleDialogListener.java
com.avast.android.dialogs.util.TypefaceHelper.java
com.avast.dialogs.DemoActivity.java
com.avast.dialogs.JayneHatDialogFragment.java