Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the size of a DialogFragment

I have been trying many commands to setup the size of my DialogFragment. It only contains a color-picker, so I have removed the background and title of the dialog:

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setBackgroundDrawable(     new ColorDrawable(android.graphics.Color.TRANSPARENT)); 

However I also want to position the dialog where I want and it is problematic. I use:

WindowManager.LayoutParams params = getDialog().getWindow().getAttributes(); params.width = LayoutParams.WRAP_CONTENT; params.height =  LayoutParams.WRAP_CONTENT; params.gravity = Gravity.LEFT; getDialog().getWindow().setAttributes(params); 

But one (big) obstacle remains: even though my dialog pane is invisible, it still has a certain size, and it limits the positions of my dialog. The LayoutParams.WRAP_CONTENT are here to limit the size of this pane to my color-picker, but for some reason it does not work.

Has anyone been able to do something similar?

like image 858
Teovald Avatar asked Feb 18 '13 23:02

Teovald


People also ask

How do I set the width and height of an AlertDialog?

show(); alertDialog. getWindow(). setLayout(600, 400); //Controlling width and height. Or you can do it in my way.

How do I change the dialog size in Android?

According to Android platform developer Dianne Hackborn in this discussion group post, Dialogs set their Window's top level layout width and height to WRAP_CONTENT . To make the Dialog bigger, you can set those parameters to MATCH_PARENT . Demo code: AlertDialog.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.


1 Answers

i met a similar question that is you can't set the dialogFragment's width an height in code,after several try ,i found a solution;

here is steps to custom DialogFragment:

1.inflate custom view from xml on method

public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState) {      getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);     getDialog().setCanceledOnTouchOutside(true);      View view = inflater.inflate(R.layout.XXX,             container, false);     //TODO:findViewById, etc     return view; } 

2.set your dialog's width an height in onResume(),remrember in onResume()/onStart(),seems didn't work in other method

public void onResume() {     super.onResume();     Window window = getDialog().getWindow();     window.setLayout(width, height);     window.setGravity(Gravity.CENTER);     //TODO: }   
like image 78
ruidge Avatar answered Sep 19 '22 09:09

ruidge