Search code examples
androidandroid-fragmentsandroid-dialogfragmentandroid-dialog

Custom Dialog Fragment Examples


I am sorry for asking this. But I have difficulties in grasping on how to create a custom DialogFragment.

So far I have only managed to create a Custom Dialog. Which is easy to do. Where we simply extends a Dialog class and we set the contentview with our custom layout.

I am wondering if anyone could provide a simple sample of a Custom DialogFragment.

I am looking for a way to create an alert dialog with multiple buttons.

Thank you


Solution

  • Actually i dont know it will help you or not.Cause i am not clear what type of view you want.But i am giving you a sample code so that you can modify as your wish..I developed for my application for dtmf functionality.Change the image and your layout xml..

    //DtmfDialogFragment.java
    
    package com.sp.zps.ui;
    
    import com.sp.zps.R;
    
    import android.annotation.SuppressLint;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.TextView;
    
    import com.actionbarsherlock.app.SherlockDialogFragment;
    
    public class DtmfDialogFragment extends SherlockDialogFragment {
    
    
        private static final String EXTRA_CALL_ID = "call_id";
        public static DtmfDialogFragment newInstance(int callId) {
            DtmfDialogFragment instance = new DtmfDialogFragment();
            Bundle args = new Bundle();
            args.putInt(EXTRA_CALL_ID, callId);
            instance.setArguments(args);
            return instance;
        }
    
    
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            return new AlertDialog.Builder(getActivity())
                    .setView(getCustomView(getActivity().getLayoutInflater(), null, savedInstanceState))
                    .setCancelable(true)
                    .setNeutralButton("Done", new OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dismiss();
                        }
                    })
                    .create();
        }
    
        EditText dialPadTextView;
        @SuppressLint("NewApi") 
        public View getCustomView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.in_call_dialpad, container, false);
            View keyboard = v.findViewById(R.id.dialkeyboard);
            dialPadTextView = (EditText) v.findViewById(R.id.digitsText);
    
    
            ImageButton[] btns = new ImageButton[12];
            setButtonView(btns, keyboard);
            for (final ImageButton btn : btns) {
                btn.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        dialButtonPressed(v);
                    }
                });
            }
            if (android.os.Build.VERSION.SDK_INT >= 11) {
                dialPadTextView.setTextIsSelectable(true);
            }
    
             return v;
        }
    
     private void  dialButtonPressed(View v){
    
         String pressedKey = "";
            switch (v.getId()) {
            case R.id.button0:
                pressedKey = "0";
                break;
            case R.id.button1:
                pressedKey = "1";
                break;
            case R.id.button2:
                pressedKey = "2";
                break;
            case R.id.button3:
                pressedKey = "3";
                break;
            case R.id.button4:
                pressedKey = "4";
                break;
            case R.id.button5:
                pressedKey = "5";
                break;
            case R.id.button6:
                pressedKey = "6";
                break;
            case R.id.button7:
                pressedKey = "7";
                break;
            case R.id.button8:
                pressedKey = "8";
                break;
            case R.id.button9:
                pressedKey = "9";
                break;
            case R.id.buttonstar:
                pressedKey = "*";
                break;
            case R.id.buttonpound:
                pressedKey = "#";
                break;
            default:
                break;
    
            }
    
            udpadatePhonePad(pressedKey);
     }
    
     private void udpadatePhonePad(String pressedkeyVal) {
    
            int pos = dialPadTextView.getSelectionStart();
            dialPadTextView.getText().insert(pos, pressedkeyVal);
    
        }
        private void setButtonView(ImageButton[] btns, View keyboard) {
    
            btns[0] = (ImageButton) keyboard.findViewById(R.id.button0);
            btns[1] = (ImageButton) keyboard.findViewById(R.id.button1);
            btns[2] = (ImageButton) keyboard.findViewById(R.id.button2);
            btns[3] = (ImageButton) keyboard.findViewById(R.id.button3);
            btns[4] = (ImageButton) keyboard.findViewById(R.id.button4);
            btns[5] = (ImageButton) keyboard.findViewById(R.id.button5);
            btns[6] = (ImageButton) keyboard.findViewById(R.id.button6);
            btns[7] = (ImageButton) keyboard.findViewById(R.id.button7);
            btns[8] = (ImageButton) keyboard.findViewById(R.id.button8);
            btns[9] = (ImageButton) keyboard.findViewById(R.id.button9);
            btns[10] = (ImageButton) keyboard.findViewById(R.id.buttonstar);
            btns[11] = (ImageButton) keyboard.findViewById(R.id.buttonpound);
    
        }
    
    }
    

    call this fragment like this..

    DtmfDialogFragment newFragment = DtmfDialogFragment.newInstance(callId);
    newFragment.show(getSupportFragmentManager(), "dialog");
    

    Hope it may help you get the correct way..