Search in sources :

Example 6 with MaterialDialog

use of com.afollestad.materialdialogs.MaterialDialog in project material-dialogs by afollestad.

the class ColorChooserDialog method onCreateDialog.

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    if (getArguments() == null || !getArguments().containsKey("builder")) {
        throw new IllegalStateException("ColorChooserDialog should be created using its Builder interface.");
    }
    generateColors();
    int preselectColor;
    boolean foundPreselectColor = false;
    if (savedInstanceState != null) {
        foundPreselectColor = !savedInstanceState.getBoolean("in_custom", false);
        preselectColor = getSelectedColor();
    } else {
        if (getBuilder().setPreselectionColor) {
            preselectColor = getBuilder().preselectColor;
            if (preselectColor != 0) {
                for (int topIndex = 0; topIndex < colorsTop.length; topIndex++) {
                    if (colorsTop[topIndex] == preselectColor) {
                        foundPreselectColor = true;
                        topIndex(topIndex);
                        if (getBuilder().accentMode) {
                            subIndex(2);
                        } else if (colorsSub != null) {
                            findSubIndexForColor(topIndex, preselectColor);
                        } else {
                            subIndex(5);
                        }
                        break;
                    }
                    if (colorsSub != null) {
                        for (int subIndex = 0; subIndex < colorsSub[topIndex].length; subIndex++) {
                            if (colorsSub[topIndex][subIndex] == preselectColor) {
                                foundPreselectColor = true;
                                topIndex(topIndex);
                                subIndex(subIndex);
                                break;
                            }
                        }
                        if (foundPreselectColor) {
                            break;
                        }
                    }
                }
            }
        } else {
            preselectColor = Color.BLACK;
            foundPreselectColor = true;
        }
    }
    circleSize = getResources().getDimensionPixelSize(R.dimen.md_colorchooser_circlesize);
    final Builder builder = getBuilder();
    MaterialDialog.Builder bd = new MaterialDialog.Builder(getActivity()).title(getTitle()).autoDismiss(false).customView(R.layout.md_dialog_colorchooser, false).negativeText(builder.cancelBtn).positiveText(builder.doneBtn).neutralText(builder.allowUserCustom ? builder.customBtn : 0).onPositive(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            callback.onColorSelection(ColorChooserDialog.this, getSelectedColor());
            dismiss();
        }
    }).onNegative(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            if (isInSub()) {
                dialog.setActionButton(DialogAction.NEGATIVE, getBuilder().cancelBtn);
                isInSub(false);
                // Do this to avoid ArrayIndexOutOfBoundsException
                subIndex(-1);
                invalidate();
            } else {
                dialog.cancel();
            }
        }
    }).onNeutral(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            toggleCustom(dialog);
        }
    }).showListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            invalidateDynamicButtonColors();
        }
    });
    if (builder.theme != null) {
        bd.theme(builder.theme);
    }
    final MaterialDialog dialog = bd.build();
    final View v = dialog.getCustomView();
    grid = (GridView) v.findViewById(R.id.md_grid);
    if (builder.allowUserCustom) {
        selectedCustomColor = preselectColor;
        colorChooserCustomFrame = v.findViewById(R.id.md_colorChooserCustomFrame);
        customColorHex = (EditText) v.findViewById(R.id.md_hexInput);
        customColorIndicator = v.findViewById(R.id.md_colorIndicator);
        customSeekA = (SeekBar) v.findViewById(R.id.md_colorA);
        customSeekAValue = (TextView) v.findViewById(R.id.md_colorAValue);
        customSeekR = (SeekBar) v.findViewById(R.id.md_colorR);
        customSeekRValue = (TextView) v.findViewById(R.id.md_colorRValue);
        customSeekG = (SeekBar) v.findViewById(R.id.md_colorG);
        customSeekGValue = (TextView) v.findViewById(R.id.md_colorGValue);
        customSeekB = (SeekBar) v.findViewById(R.id.md_colorB);
        customSeekBValue = (TextView) v.findViewById(R.id.md_colorBValue);
        if (!builder.allowUserCustomAlpha) {
            v.findViewById(R.id.md_colorALabel).setVisibility(View.GONE);
            customSeekA.setVisibility(View.GONE);
            customSeekAValue.setVisibility(View.GONE);
            customColorHex.setHint("2196F3");
            customColorHex.setFilters(new InputFilter[] { new InputFilter.LengthFilter(6) });
        } else {
            customColorHex.setHint("FF2196F3");
            customColorHex.setFilters(new InputFilter[] { new InputFilter.LengthFilter(8) });
        }
        if (!foundPreselectColor) {
            // If color wasn't found in the preset colors, it must be custom
            toggleCustom(dialog);
        }
    }
    invalidate();
    return dialog;
}
Also used : InputFilter(android.text.InputFilter) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DialogInterface(android.content.DialogInterface) GridView(android.widget.GridView) View(android.view.View) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) DialogAction(com.afollestad.materialdialogs.DialogAction) NonNull(android.support.annotation.NonNull) NonNull(android.support.annotation.NonNull)

Example 7 with MaterialDialog

use of com.afollestad.materialdialogs.MaterialDialog in project material-dialogs by afollestad.

the class FileChooserDialog method onSelection.

@Override
public void onSelection(MaterialDialog materialDialog, View view, int i, CharSequence s) {
    if (canGoUp && i == 0) {
        parentFolder = parentFolder.getParentFile();
        if (parentFolder.getAbsolutePath().equals("/storage/emulated")) {
            parentFolder = parentFolder.getParentFile();
        }
        canGoUp = parentFolder.getParent() != null;
    } else {
        parentFolder = parentContents[canGoUp ? i - 1 : i];
        canGoUp = true;
        if (parentFolder.getAbsolutePath().equals("/storage/emulated")) {
            parentFolder = Environment.getExternalStorageDirectory();
        }
    }
    if (parentFolder.isFile()) {
        callback.onFileSelection(this, parentFolder);
        dismiss();
    } else {
        parentContents = listFiles(getBuilder().mimeType, getBuilder().extensions);
        MaterialDialog dialog = (MaterialDialog) getDialog();
        dialog.setTitle(parentFolder.getAbsolutePath());
        getArguments().putString("current_path", parentFolder.getAbsolutePath());
        dialog.setItems(getContentsArray());
    }
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog)

Example 8 with MaterialDialog

use of com.afollestad.materialdialogs.MaterialDialog in project material-dialogs by afollestad.

the class FolderChooserDialog method createNewFolder.

private void createNewFolder() {
    new MaterialDialog.Builder(getActivity()).title(getBuilder().newFolderButton).input(0, 0, false, new MaterialDialog.InputCallback() {

        @Override
        public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
            //noinspection ResultOfMethodCallIgnored
            final File newFi = new File(parentFolder, input.toString());
            if (!newFi.mkdir()) {
                String msg = "Unable to create folder " + newFi.getAbsolutePath() + ", make sure you have the WRITE_EXTERNAL_STORAGE permission or root permissions.";
                Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
            } else {
                reload();
            }
        }
    }).show();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) NonNull(android.support.annotation.NonNull) File(java.io.File)

Example 9 with MaterialDialog

use of com.afollestad.materialdialogs.MaterialDialog in project material-dialogs by afollestad.

the class FolderChooserDialog method onCreateDialog.

@SuppressWarnings("ConstantConditions")
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        return new MaterialDialog.Builder(getActivity()).title(R.string.md_error_label).content(R.string.md_storage_perm_error).positiveText(android.R.string.ok).build();
    }
    if (getArguments() == null || !getArguments().containsKey("builder")) {
        throw new IllegalStateException("You must create a FolderChooserDialog using the Builder.");
    }
    if (!getArguments().containsKey("current_path")) {
        getArguments().putString("current_path", getBuilder().initialPath);
    }
    parentFolder = new File(getArguments().getString("current_path"));
    checkIfCanGoUp();
    parentContents = listFiles();
    MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity()).title(parentFolder.getAbsolutePath()).items((CharSequence[]) getContentsArray()).itemsCallback(this).onPositive(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            dialog.dismiss();
            callback.onFolderSelection(FolderChooserDialog.this, parentFolder);
        }
    }).onNegative(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            dialog.dismiss();
        }
    }).autoDismiss(false).positiveText(getBuilder().chooseButton).negativeText(getBuilder().cancelButton);
    if (getBuilder().allowNewFolder) {
        builder.neutralText(getBuilder().newFolderButton);
        builder.onNeutral(new MaterialDialog.SingleButtonCallback() {

            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                createNewFolder();
            }
        });
    }
    if ("/".equals(getBuilder().initialPath)) {
        canGoUp = false;
    }
    return builder.build();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DialogAction(com.afollestad.materialdialogs.DialogAction) NonNull(android.support.annotation.NonNull) File(java.io.File) NonNull(android.support.annotation.NonNull)

Example 10 with MaterialDialog

use of com.afollestad.materialdialogs.MaterialDialog in project material-dialogs by afollestad.

the class MaterialDialogPreference method showDialog.

@Override
protected void showDialog(Bundle state) {
    MaterialDialog.Builder builder = new MaterialDialog.Builder(context).title(getDialogTitle()).icon(getDialogIcon()).dismissListener(this).onAny(new MaterialDialog.SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            switch(which) {
                default:
                    MaterialDialogPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    break;
                case NEUTRAL:
                    MaterialDialogPreference.this.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                    break;
                case NEGATIVE:
                    MaterialDialogPreference.this.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                    break;
            }
        }
    }).positiveText(getPositiveButtonText()).negativeText(getNegativeButtonText()).autoDismiss(// immediately close the dialog after selection
    true);
    final View contentView = onCreateDialogView();
    if (contentView != null) {
        onBindDialogView(contentView);
        builder.customView(contentView, false);
    } else {
        builder.content(getDialogMessage());
    }
    PrefUtil.registerOnActivityDestroyListener(this, this);
    dialog = builder.build();
    if (state != null) {
        dialog.onRestoreInstanceState(state);
    }
    dialog.show();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DialogAction(com.afollestad.materialdialogs.DialogAction) NonNull(android.support.annotation.NonNull) View(android.view.View)

Aggregations

MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)80 View (android.view.View)49 DialogAction (com.afollestad.materialdialogs.DialogAction)44 NonNull (android.support.annotation.NonNull)32 TextView (android.widget.TextView)32 Intent (android.content.Intent)19 ImageView (android.widget.ImageView)19 RecyclerView (android.support.v7.widget.RecyclerView)17 File (java.io.File)17 MenuItem (android.view.MenuItem)14 PopupMenu (android.widget.PopupMenu)12 List (java.util.List)12 ListView (android.widget.ListView)11 Activity (android.app.Activity)9 Toast (android.widget.Toast)9 SuppressLint (android.annotation.SuppressLint)8 Context (android.content.Context)8 Bundle (android.os.Bundle)8 ArrayList (java.util.ArrayList)8 LayoutInflater (android.view.LayoutInflater)7