Search code examples
androidandroid-dialogfragmentdialogfragment

Android - Floating Alert Dialog which still enables user input in the main layout/activity


Good day, apologies for the confusing title.

I am creating an android application and I need a dialog/pop up to appear at a specific X Y Position. I already have a working DialogFragmet as shown below:

public class ItemDialog extends DialogFragment implements OnClickListener{

    //a lot of public and private variables here

    public interface onSubmitListener {
        void setOnSubmitListener(String qty, String disc, String instructions, UOMClass uom);  
    }  

    @Override  
    public Dialog onCreateDialog(Bundle savedInstanceState) {  

        final Dialog dialog = new Dialog(getActivity());  
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  

        LayoutInflater layoutInflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout2 = layoutInflater.inflate(R.layout.fragment_item_dialog, null);

        dialog.setContentView(layout2);  
        setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);

        //snip snip more code   

        dialog.show();  

        //buttons - I set the buttons here

        //editTexts - I set the edit texts here

        //TextViews - I set the text views here

        return dialog;  
    }  

    //more functions here

}

And I call it from my base activity as follows:

ItemDialog itemDialog = new ItemDialog();

//pass values from base activity to the ItemDialog here

itemDialog.show(getFragmentManager(), "");

However, I need to be able to see the base activity and input values in the edit texts in it. As of now, my DialogFragment covers a huge part of the screen and I'm unable to see the activity behind it. Clicking outside the Dialog Fragment cancels it as well.

I've searched on how to make an alert dialog float but to no avail.

Is there a way to display a Dialog Fragment over a certain area of the screen to allow the user to see and still input values in the base activity?

Any help is very much appreciated, thank you.


Solution

  • you can get window object and set it's layout parameters below code might help you.

    Window window = getDialog().getWindow();
    
        // set "origin" to top left corner, so to speak
        window.setGravity(Gravity.TOP|Gravity.LEFT);
    
        // after that, setting values for x and y works "naturally"
        WindowManager.LayoutParams params = window.getAttributes();
        params.x = 300;
        params.y = 100;
        window.setAttributes(params);
    

    for more info Position of DialogFragment in Android you can also set background color or bitmap to dialog