3

I have created an AlertDialog (like below) and would like to display a View (floating action button from library) over it.

new AlertDialog.Builder(getActivity())
    .setTitle("Last Day of School Description")
.setMessage(...)
.show();

It currently looks like this, but the dialog overlays the button. I want the button to be above the AlertDialog and clickable as well. Android Dialog Overlay My FloatingActonButton object is called "fab".

2 Answers2

0

What you need to do is to create your own custom view and attach it in your AlertDialog to have full control of the layout of the dialog itself

You can go here for more info on how to implement a custom view.

Community
  • 1
  • 1
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

You have to make a custom layout for dialog in order to include buttons of your choice. Below is the example of how you can make it work:

public void showCustomDialog(){
 try{
    final Dialog d = new Dialog(act);
    d.setContentView(R.layout.custom_layout_fordialog); // your custom dialog layout
    d.setCancelable(false);


    TextView status = (TextView) d.findViewById(R.id.result_status);
    status.setText("Title of dialog");

    TextView msg = (TextView) d.findViewById(R.id.result_msg);
    msg.setText("body of dialog");

    Button ok = (Button) d.findViewById(R.id.result_ok); // your button



    ok.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // do whatever you want here

        }
    });
    d.show();
    }
    catch(Exception e){
        e.printStackTrace();
    }

} // method end

-EDIT-

Please note that the above example is an alternative to AlertDialog . You cannot use this as a child view in AlertDialog

Sharp Edge
  • 4,144
  • 2
  • 27
  • 41
  • This is not working, my result is the button inside of the dialog, rather than in the bottom right hand corner. –  May 12 '15 at 01:07
  • @apoorvk please design the layout yourself, as how you want the dialog to be. Can't you design a layout in XML ? and then set the layout in `d.setContentView(R.layout.YOUR_LAYOUT_FOR_DIALOG)` there is no rocket science behind it. – Sharp Edge May 12 '15 at 06:09
  • I already did, but my issue is that you cannot create a child view outside of the parent (dialog). Do you know any alternatives to make this work? –  May 12 '15 at 06:11
  • @apoorvk my solution above does not expect you to make this as a child in `AlertDialog` . Its an alternative for the whole `AlertDialog` just remove the `AlertDialog` code and call this `Dialog` instead... in My Case it works as I want, I have Vertical Buttons set to my custom layout.. – Sharp Edge May 12 '15 at 06:14