Search code examples
android-dialogfragmentdialogfragment

How to define gravity of a Dialog Fragment?


I want my dialog to stick to the start but it always comes in the center.


Solution

  • i will give you example==>

    Following is the code -->Onclick image -->Dialog Appears==>

        Dialog dialog; //your dialog declaration
         // 
        //
        //
        oncreate(){
    
    
        imageView = (ImageView) findViewById(R.id.customProfileGridImg);
        dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.activity);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
    
        wlp.gravity = Gravity.START |Gravity.TOP; //change direction as per your requirement
        wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(wlp);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.show();
    
            }
        });
        }