Vikas Rohilla
Vikas Rohilla

Reputation: 41

Android : Dialog occupy 100% width

 final Dialog dialog = new Dialog(this, R.style.theme_dialog);
 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
 dialog.setContentView(R.layout.dialog_name);
 dialog.setCancelable(false);

 dialog.getWindow().setGravity(Gravity.TOP);
 dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

Above code occupy around 90% width but I want 100 percent.

Upvotes: 3

Views: 11116

Answers (5)

Mrudul Tora
Mrudul Tora

Reputation: 768

Firstly,use RelativeLayout as the parent view of custom dialog, then set the width to match_parent and height to wrap_content in the xml file of your custom dialog. Then use this code in java file...

Dialog dialog=new Dialog(AllProductListActivity.this);
dialog.setContentView(R.layout.your_custom_dialog_layout);                
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));             
dialog.getWindow().setLayout.(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.show();    

As you will read many solutions only used setLayout(), that alone cannot make the width of dialog to match_parent. But here,on adding the below line the width of dialog will be match_parent while the height will be wrap_content.

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));                 

Upvotes: 1

IldiX
IldiX

Reputation: 601

Try to use a RelativeLayout as the root view of your dialog.

Upvotes: 0

Paras Valera
Paras Valera

Reputation: 492

Try following code, this will solve your issue.

    //Grab the window of the dialog, and change the width
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    Window window = dialog.getWindow();
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    lp.copyFrom(window.getAttributes());
    //This makes the dialog take up the full width
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    window.setAttributes(lp);

Upvotes: 6

Pavya
Pavya

Reputation: 6025

Add below style

<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog">
        <item name="android:windowMinWidthMajor">100%</item>
        <item name="android:windowMinWidthMinor">100%</item>
</style> 

and

final Dialog dialog = new Dialog(getActivity(), R.style.Theme_Dialog);

Upvotes: 12

Harshad Pansuriya
Harshad Pansuriya

Reputation: 20930

Try to change this

 dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

to this

 dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT);

Upvotes: 3

Related Questions