-1

In my application i have created a simple custom dialog. I have to call that in a fragment class.Dont know how to proceed can anyone help me with this? Is there any way that we can use simple dialog in fragment without going for Dialogfragment?

june
  • 999
  • 3
  • 14
  • 20

1 Answers1

0

using dialog fragment would be the better practice.

Dialog Fragment They have given different dialog fragment concepts will match all the needs related to dialog. other than this

Simple alert dialog in fragment

AlertDialog ad = new AlertDialog.Builder(getActivity())
        .create();
ad.setCancelable(false);
ad.setTitle(title);
ad.setMessage(message);
ad.setButton(context.getString(R.string.ok_text), new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});

ad.show();

custom dialog in fragment

        final Dialog dialog = new Dialog(getActivity);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

UPDATE 1 :

Here i give u one example that i used to show date picker using dialog fragment

public static class SelectDateFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }

    @Override
    public void onDateSet(DatePicker view, int yy, int mm, int dd) {

        dobString = myYear + "-" + myMonth + "-" + myDay;
        populateSetDate(yy, mm + 1, dd);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {

        super.onDismiss(dialog);
    }

}

And to show the dialog

public void selectDate(View view) {
    DialogFragment newFragment = new SelectDateFragment();
    newFragment.show(getSupportFragmentManager(), "DatePicker");
}

which will be called from a button click..

Tamilselvan Kalimuthu
  • 1,534
  • 1
  • 13
  • 28
  • well i have created a custom dialog as a separate class and that i have to call as a method in fragment class. How to implement that? Please help me. – june Jan 10 '14 at 11:04
  • 1
    what is the need to create a sepereate class. if u may reuse that class for some other funcationality it is fine, otherwise u can keep within the method to execute , i update with one example for u. – Tamilselvan Kalimuthu Jan 10 '14 at 11:13
  • well is there any way we can use only dialog without going for dialogfragment in fragment class. If so how? In the above snippet you have used dialogfragment but i dont want to extend dialogfragment instead i have extend only dialog and that i have to call as a method in fragment class. – june Jan 10 '14 at 11:29
  • custom dialog in fragment i have given above see my ans. – Tamilselvan Kalimuthu Jan 10 '14 at 11:30