Change Size Dialogfragment according to Android screen

Asked

Viewed 920 times

1

I have a very simple registration form that appears when you click a sign up button. This form appears in a DialogFragment, however, with a very small size.
Zoning 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 apparently he sets these values in pixels, so I wanted to know if there is a way to calculate the width of the screen (or view I don’t know) and set the width at 80% of the screen and let the height increase automatically.

Note: I know it’s a bit confused my words but I can understand my goal with this question, in case I didn’t understand just make a comment that I explain in more detail.

1 answer

3


A Solution can be to take the dimensions of the screen of the apparatus.

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

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

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

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

  • Width worked but then it’s mandatory to put height, and I wanted it to automatically increase height (something like wrap_parent).

  • And in line . getSystemService the android studio shows the non-static error method getSystemService(String) cannot be referenced from a Static context

  • I adjusted the code above, with respect to height, would change based on some event?

  • No, I have a registration form with a title Register, and a Textview and Edittext for the fields Name, Login, Password and Repeat Password, plus two buttons one to cancel the registration and another to register.

  • So the width has to be 80% of the screen but the height has to increase by itself. I would have some idea of how to do this?

  • So the height of the dialog would have to be the sum of the components inside it? Something like that?

  • Exactly, just like when we set the height of a layout with wrap_parent.

  • 1

    When you mount the dialog, you inflate it from a view? you can set the height straight in the view. Post the dialog code to take a look.

  • My man, I can’t believe I didn’t do this, is that as I’m starting on android, I’m not sure what can and can’t do.

  • See help: http://stackoverflow.com/questions/21545728/dialog-fragment-with-wrap-content-coming-as-a-full-screen-how-to-make-it-as-a-h

  • And I thought that with Dialogfragment there would be no way to define a direct height in the layout because I had read something similar in a forum

  • Great link, I’ll take a closer look at it, obg.

  • To do yes, this link I think is clearer for you to implement: http://stackoverflow.com/questions/12478520/how-to-dialogset-dialogfragments-width-and-height. anything post there.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.