Change DialogFragment size according to Android screen

1

I have a very simple registration form that appears when I click a button to register. This form appears in DialogFragment , however, with a very small size.
Zonzando on the internet I found a code that changes the size of it:


  @Override
  public void onStart() {
        super.onStart();

    if (getDialog() == null)
        return;

    int dialogWidth = 200;
    int dialogHeight = 400;

    getDialog().getWindow().setLayout(dialogWidth, dialogHeight);

 } 

But it seems that it defines these values in pixels, so I wanted to know if there is a way to calculate the width of the screen (or view I do not know) and set the width to 80% of the screen and let the height increase automatically.

Note: I'm a little confused by my words but I can understand my purpose with this question, if you do not understand, just make a comment that I explain in more detail.

    
asked by anonymous 31.03.2016 / 21:44

1 answer

3

A solution can be to take the dimensions of the device's screen.

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int largura = size.x;
int altura = size.y;

And from there, perform the calculation for the height / width of your Dialog.

larguraDoSeuDialog = largura * 0.8;
alturadoSeuDialog = altura * 0.8;

Note: The Context above would be an instance of your activity, if you are running the process inside it, it may be this, otherwise you will have to pass the parameter.

    
31.03.2016 / 22:14