Example usage for android.app Dialog setOnShowListener

List of usage examples for android.app Dialog setOnShowListener

Introduction

In this page you can find the example usage for android.app Dialog setOnShowListener.

Prototype

public void setOnShowListener(@Nullable OnShowListener listener) 

Source Link

Document

Sets a listener to be invoked when the dialog is shown.

Usage

From source file:hku.fyp14017.blencode.ui.dialogs.TextDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    View dialogView = LayoutInflater.from(getActivity())
            .inflate(hku.fyp14017.blencode.R.layout.dialog_text_dialog, null);
    input = (EditText) dialogView.findViewById(hku.fyp14017.blencode.R.id.dialog_text_edit_text);
    inputTitle = (TextView) dialogView.findViewById(hku.fyp14017.blencode.R.id.dialog_text_text_view);

    if (getHint() != null) {
        input.setHint(getHint());//  w ww  . j a  v  a 2  s  . co m
    }

    input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus) {
                getDialog().getWindow()
                        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });

    initialize();

    Dialog dialog = new AlertDialog.Builder(getActivity()).setView(dialogView).setTitle(getTitle())
            .setNegativeButton(hku.fyp14017.blencode.R.string.cancel_button, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dismiss();
                }
            }).setPositiveButton(hku.fyp14017.blencode.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            }).create();

    dialog.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
                boolean okButtonResult = handleOkButton();
                onOkButtonHandled();
                if (okButtonResult) {
                    dismiss();
                }
                return okButtonResult;
            }

            return false;
        }
    });

    dialog.setCanceledOnTouchOutside(true);
    dialog.setOnShowListener(new OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            Button buttonPositive = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
            buttonPositive.setEnabled(getPositiveButtonEnabled());

            setPositiveButtonClickCustomListener();

            InputMethodManager inputManager = (InputMethodManager) getActivity()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);

            initTextChangedListener();
        }
    });

    return dialog;
}

From source file:hku.fyp14017.blencode.ui.dialogs.UploadProjectDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    View dialogView = LayoutInflater.from(getActivity())
            .inflate(hku.fyp14017.blencode.R.layout.dialog_upload_project, null);

    projectRename = (TextView) dialogView.findViewById(hku.fyp14017.blencode.R.id.tv_project_rename);
    projectDescriptionField = (EditText) dialogView
            .findViewById(hku.fyp14017.blencode.R.id.project_description_upload);
    projectUploadName = (EditText) dialogView.findViewById(hku.fyp14017.blencode.R.id.project_upload_name);
    sizeOfProject = (TextView) dialogView
            .findViewById(hku.fyp14017.blencode.R.id.dialog_upload_size_of_project);

    Dialog dialog = new AlertDialog.Builder(getActivity()).setView(dialogView)
            .setTitle(hku.fyp14017.blencode.R.string.upload_project_dialog_title)
            .setPositiveButton(hku.fyp14017.blencode.R.string.upload_button,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            handleUploadButtonClick();
                        }/*from   w ww. j  ava  2  s .co  m*/
                    })
            .setNegativeButton(hku.fyp14017.blencode.R.string.cancel_button,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            handleCancelButtonClick();
                        }
                    })
            .create();

    dialog.setCanceledOnTouchOutside(true);
    dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    dialog.setOnShowListener(new OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            initListeners();

            InputMethodManager inputManager = (InputMethodManager) getActivity()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(projectUploadName, InputMethodManager.SHOW_IMPLICIT);
        }
    });

    initControls();

    return dialog;
}