0

I want to achieve the following design: A dialog with custom layout, created programatically. It will contain an EditText and a Button. I want the soft keyboard to pop up when the dialog appears, and I want the dialog to fill the screen horizontally and to be placed right above the keyboard.

Here is what I've done right now:

                final AlertDialog obsDialog = new AlertDialog.Builder(ProdutoDetalheActivity.this).create();
                final View obsLayout = View.inflate(getApplicationContext(), R.layout.observation_layout, null);
                Button obsButton = (Button) obsLayout.findViewById(R.id.observation_button);
                obsEdit = (EditText) obsLayout.findViewById(R.id.observation_edit);
                obsButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        text.setText(String.valueOf(obsEdit.getText()));
                        obsDialog.dismiss();
                    }
                });
                obsDialog.setView(obsLayout);
                obsDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
                obsDialog.show();
                obsEdit.requestFocus();

But this doesn't makes the dialog full width, nor calls the soft keyboard. And I still wonder how can I align the dialog with the keyboard.

I've tried these answers with no success.

Thanks in advance for any help!

[EDIT] I've brought the keyboard up by using the following code:

obsDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
obsDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

[EDIT] Below is a printscreen of the design I want to achieve: Image 1

Community
  • 1
  • 1
Lucas Jota
  • 1,863
  • 4
  • 24
  • 43
  • 2
    Is it even a dialog at that point? You could just use a transparent `Activity` with `softInputMode="adjustResize"`. – Kevin Coppock Sep 25 '14 at 15:55
  • I'll try that. I want the edittext to dismiss if user touches outside it.. and when it appears the activity on background should be "darkened". – Lucas Jota Sep 25 '14 at 17:09

1 Answers1

1

As far as I know, you have to adjust the dialog size/position in the code if you do not want the default size/position.

In you case specifically, you can fist declare adjustResize for the activity's windowSoftInputMode in the AndroidManifest.xml.

After that, you can set your dialog gravity at bottom and set the width in the onStart of the dialog fragment:

  ...
  getDialog().getWindow().setLayout(mWidth, mHeight);
  WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
  lp.gravity = Gravity.BOTTOM;
  lp.x = 0; lp.y = 0;
  getDialog().getWindow().setAttributes(lp);
  ...

the mWidth and mHeight are initialized in onCreate, but I think you can do it in the onStart as well. To get the width, I used below code:

  Point size = new Point();
  WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
  Display display = wm.getDefaultDisplay();
  display.getSize(size);
  mWidth = size.x;

Hope this can meet your requirement. I used this to create a similar dialog, but I do not have a edit in the dialog. And I need it to be at the bottom of the screen.

Qianqian
  • 2,104
  • 18
  • 28
  • `display.getSize()` requires API 13, while I need a solution that works at API 9. Nonetheless setting the `Gravity` and the `windowSoftInputMode` did place the dialog above the keyboard... Now there's still some margin at the dialog I can't get rid of – Lucas Jota Sep 30 '14 at 17:26
  • For API level 9 I think there is another API that can get the window width, you can have a try on google. what is the margin you mentioned? the vertical one or horizontal one? You need to set the dialog style to no title, no border as well. – Qianqian Oct 02 '14 at 01:06
  • @Qianqian I still have margins on all directions, will upload a pic asap. I'm accepting this answer so you can get the bounty, thanks for your attention. – Lucas Jota Oct 02 '14 at 15:12
  • @Lucas,thank you. I will soon try the approach as well to check it as now i have not got a computer at my hand – Qianqian Oct 04 '14 at 12:42
  • Hi Lucas, I tried the approach and I did not get the margins. I guess you need to adjust the layout file. – Qianqian Oct 06 '14 at 01:47
  • I could set the position of the dialog fragment with this approach (which I already knew) but could not set the width. The width, regardless of what I do, is always a certain percentage of the portrait width. – Brian Reinhold Mar 17 '15 at 10:05