Example usage for android.app AlertDialog.Builder AlertDialog.Builder

List of usage examples for android.app AlertDialog.Builder AlertDialog.Builder

Introduction

In this page you can find the example usage for android.app AlertDialog.Builder AlertDialog.Builder.

Prototype

public Builder(Context context) 

Source Link

Document

Creates a builder for an alert dialog that uses the default alert dialog theme.

Usage

From source file:net.nightwhistler.pageturner.activity.ReadingFragment.java

private void showSearchResultDialog(final List<SearchResult> results) {

    if (isSearchResultsDialogShowing) {
        return;/*from w  w  w.  ja  va 2s.  c  o m*/
    }

    isSearchResultsDialogShowing = true;

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(R.string.search_results);

    SearchResultAdapter adapter = new SearchResultAdapter(context, bookView, results);
    builder.setAdapter(adapter, adapter);

    AlertDialog dialog = builder.create();
    dialog.setOwnerActivity(getActivity());
    dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            isSearchResultsDialogShowing = false;
        }
    });
    dialog.show();
}

From source file:com.free.searcher.MainFragment.java

@Override
public boolean onQueryTextSubmit(String query) {
    currentSearching = query;// w  ww . ja va 2s. co  m
    findBox.setText(currentSearching);
    statusView.setText("Query = '" + query + "' : submitted");
    webView.requestFocus();
    try {
        // cha load source files
        if (query.trim().length() == 0) {
            status = "Nothing to search. Please type something for searching";
            showToast(status.toString());
            statusView.setText(status);
            return true;
        }

        Log.d("selectedFiles", selectedFiles + "");
        if (selectedFiles == null || selectedFiles.length == 0 || selectedFiles[0].length() == 0) { // (cache ==
            // null &&
            // (!new
            // File(folderStr).exists()))
            status = "No file to search. Please select files or folders for searching";
            showToast(status.toString());
            statusView.setText(status);
            Log.d("search", "No file to search");
            return true;
        }

        if (getSourceFileTask == null) { // Zip Reader or Restart app
            if (currentZipFileName.length() > 0) { // Zip reader
                AlertDialog.Builder alert = new AlertDialog.Builder(activity);
                alert.setTitle("Searching files");
                alert.setMessage("Do you want to search this file " + currentZipFileName + "?");
                alert.setCancelable(true);
                alert.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToast("Start loading files, please wait...");
                        requestSearching = true;
                        getSourceFileTask = new GetSourceFileTask(MainFragment.this);
                        getSourceFileTask.execute();
                    }
                });
                alert.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alertDialog = alert.create();
                alertDialog.show();
            } else { // SEARCH Dup Comp
                showToast("Start loading files, please wait...");
                requestSearching = true;
                getSourceFileTask = new GetSourceFileTask(MainFragment.this);
                getSourceFileTask.execute();
            }
        } else if (getSourceFileTask.getStatus() == Status.RUNNING
                || getSourceFileTask.getStatus() == Status.PENDING) {
            showToast("Still loading files, please wait...");
            requestSearching = true;
        } else if (searchTask == null || searchTask.getStatus() == Status.FINISHED) {
            // load ri nhng cha chy g
            searchView(query);
        } else if (searchTask.getStatus() == Status.RUNNING || searchTask.getStatus() == Status.PENDING) {
            //  load source file nhng ang chy ci khc
            showToast("Stopping previous seaching, please wait...");
            searchTask.cancel(true);
            searchTask = null;
            searchView(query);
        }
    } catch (Throwable e) {
        Log.d("onQueryTextSubmit()", e.getMessage(), e);
        statusView.setText(e.getMessage());
    }
    return true;
}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public Object showNativePicker(final int type, final Component source, final Object currentValue,
        final Object data) {
    if (getActivity() == null) {
        return null;
    }/*from ww w  .ja  v a 2 s  .  c o m*/
    final boolean[] canceled = new boolean[1];
    final boolean[] dismissed = new boolean[1];

    if (editInProgress()) {
        stopEditing(true);
    }
    if (type == Display.PICKER_TYPE_TIME) {

        class TimePick
                implements TimePickerDialog.OnTimeSetListener, TimePickerDialog.OnCancelListener, Runnable {
            int result = ((Integer) currentValue).intValue();

            public void onTimeSet(TimePicker tp, int hour, int minute) {
                result = hour * 60 + minute;
                dismissed[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            public void run() {
                while (!dismissed[0]) {
                    synchronized (this) {
                        try {
                            wait(50);
                        } catch (InterruptedException er) {
                        }
                    }
                }
            }

            @Override
            public void onCancel(DialogInterface di) {
                dismissed[0] = true;
                canceled[0] = true;
                synchronized (this) {
                    notify();
                }
            }
        }
        final TimePick pickInstance = new TimePick();
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                int hour = ((Integer) currentValue).intValue() / 60;
                int minute = ((Integer) currentValue).intValue() % 60;
                TimePickerDialog tp = new TimePickerDialog(getActivity(), pickInstance, hour, minute, true) {

                    @Override
                    public void cancel() {
                        super.cancel();
                        dismissed[0] = true;
                        canceled[0] = true;
                    }

                    @Override
                    public void dismiss() {
                        super.dismiss();
                        dismissed[0] = true;
                    }

                };
                tp.setOnCancelListener(pickInstance);
                //DateFormat.is24HourFormat(activity));
                tp.show();
            }
        });
        Display.getInstance().invokeAndBlock(pickInstance);
        if (canceled[0]) {
            return null;
        }
        return new Integer(pickInstance.result);
    }
    if (type == Display.PICKER_TYPE_DATE) {
        final java.util.Calendar cl = java.util.Calendar.getInstance();
        if (currentValue != null) {
            cl.setTime((Date) currentValue);
        }
        class DatePick
                implements DatePickerDialog.OnDateSetListener, DatePickerDialog.OnCancelListener, Runnable {
            Date result = (Date) currentValue;

            public void onDateSet(DatePicker dp, int year, int month, int day) {
                java.util.Calendar c = java.util.Calendar.getInstance();
                c.set(java.util.Calendar.YEAR, year);
                c.set(java.util.Calendar.MONTH, month);
                c.set(java.util.Calendar.DAY_OF_MONTH, day);
                result = c.getTime();
                dismissed[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            public void run() {
                while (!dismissed[0]) {
                    synchronized (this) {
                        try {
                            wait(50);
                        } catch (InterruptedException er) {
                        }
                    }
                }
            }

            public void onCancel(DialogInterface di) {
                result = null;
                dismissed[0] = true;
                canceled[0] = true;
                synchronized (this) {
                    notify();
                }
            }
        }
        final DatePick pickInstance = new DatePick();
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                DatePickerDialog tp = new DatePickerDialog(getActivity(), pickInstance,
                        cl.get(java.util.Calendar.YEAR), cl.get(java.util.Calendar.MONTH),
                        cl.get(java.util.Calendar.DAY_OF_MONTH)) {

                    @Override
                    public void cancel() {
                        super.cancel();
                        dismissed[0] = true;
                        canceled[0] = true;
                    }

                    @Override
                    public void dismiss() {
                        super.dismiss();
                        dismissed[0] = true;
                    }

                };
                tp.setOnCancelListener(pickInstance);
                tp.show();
            }
        });
        Display.getInstance().invokeAndBlock(pickInstance);
        return pickInstance.result;
    }
    if (type == Display.PICKER_TYPE_STRINGS) {
        final String[] values = (String[]) data;
        class StringPick implements Runnable, NumberPicker.OnValueChangeListener {
            int result = -1;

            StringPick() {
            }

            public void run() {
                while (!dismissed[0]) {
                    synchronized (this) {
                        try {
                            wait(50);
                        } catch (InterruptedException er) {
                        }
                    }
                }
            }

            public void cancel() {
                dismissed[0] = true;
                canceled[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            public void ok() {
                canceled[0] = false;
                dismissed[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            @Override
            public void onValueChange(NumberPicker np, int oldVal, int newVal) {
                result = newVal;
            }
        }

        final StringPick pickInstance = new StringPick();
        for (int iter = 0; iter < values.length; iter++) {
            if (values[iter].equals(currentValue)) {
                pickInstance.result = iter;
                break;
            }
        }
        if (pickInstance.result == -1 && values.length > 0) {
            // The picker will default to showing the first element anyways
            // If we don't set the result to 0, then the user has to first
            // scroll to a different number, then back to the first option
            // to pick the first option.
            pickInstance.result = 0;
        }

        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                NumberPicker picker = new NumberPicker(getActivity());
                if (source.getClientProperty("showKeyboard") == null) {
                    picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
                }
                picker.setMinValue(0);
                picker.setMaxValue(values.length - 1);
                picker.setDisplayedValues(values);
                picker.setOnValueChangedListener(pickInstance);
                if (pickInstance.result > -1) {
                    picker.setValue(pickInstance.result);
                }
                RelativeLayout linearLayout = new RelativeLayout(getActivity());
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
                RelativeLayout.LayoutParams numPicerParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                numPicerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

                linearLayout.setLayoutParams(params);
                linearLayout.addView(picker, numPicerParams);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
                alertDialogBuilder.setView(linearLayout);
                alertDialogBuilder.setCancelable(false)
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                pickInstance.ok();
                            }
                        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                                pickInstance.cancel();
                            }
                        });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });
        Display.getInstance().invokeAndBlock(pickInstance);
        if (canceled[0]) {
            return null;
        }
        if (pickInstance.result < 0) {
            return null;
        }
        return values[pickInstance.result];
    }
    return null;
}