Note that there are some explanatory texts on larger screens.

plurals
  1. POAlertDialog with custom view doesn't "resize"
    primarykey
    data
    text
    <p>I'm facing a problem with the AlertDialog and custom content / view. Simply said the AlertDialog doesn't resize itself when the softkeyboard is opened. The following screenshots show best what my problem is and what I want to achieve:</p> <p>Current behavior (left) &amp; wanted behavior (right)</p> <p><img src="https://i.stack.imgur.com/sACwQ.png" alt="Current behavior"> <img src="https://i.stack.imgur.com/K34xh.png" alt="wanted behavior"></p> <p>I know there a some other threads with a similar issue on SO. Unfortunately none of the provided solutions worked for me. Following my sample code:</p> <p>XML</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" &gt; &lt;EditText android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;/EditText&gt; &lt;/LinearLayout&gt; &lt;/ScrollView&gt; </code></pre> <p>Java - CustomAlertDialog.class</p> <pre><code>public class CustomAlertDialog extends AlertDialog{ private Context context; public CustomAlertDialog(Context context) { super(context); this.context = context; } public void buildDialog(){ LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.dialog_content, null); builder = new AlertDialog.Builder(context); builder.setTitle("EditText"); builder.setView(layout); builder.setPositiveButton("Ok", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { dismiss(); } }); builder.create(); } public void showDialog(){ builder.show(); } } </code></pre> <p>The class / function above is called on a button press in my Main.java class</p> <pre><code>btnAlertDialog.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { CustomAlertDialog dialog = new CustomAlertDialog(Main.this); dialog.buildDialog(); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); dialog.showDialog(); } }); </code></pre> <p>I've tried the following things:</p> <p>Adding scrollbars to the LinearLayout like this</p> <pre><code>android:scrollbars="vertical" android:scrollbarAlwaysDrawVerticalTrack="true" </code></pre> <p>Result: nothing changed</p> <p>Setting a flag for the dialog:</p> <pre><code>dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); </code></pre> <p>Result: nothing changed</p> <p>Applying a custom-style to the AlertDialog:</p> <pre><code>&lt;style name="AlertDialog" parent="@android:style/Theme.Holo.Light"&gt; &lt;item name="android:windowFullscreen"&gt;false&lt;/item&gt; &lt;/style&gt; </code></pre> <p>Java</p> <pre><code>builder = new AlertDialog.Builder(context, R.style.AlertDialog); </code></pre> <p>This worked. Unfortunately it produced some problems which you can see the in the following screenshot</p> <p><img src="https://i.stack.imgur.com/JwsyB.png" alt="Custom style - new problem"></p> <p>I appreciate any help since I've been struggling with this issue for days. Thanks!</p> <h1><strong>Solution</strong></h1> <p>I'm now using the following method to create the Dialog in my <code>CustomAlertDialog.class</code>. See my comment below the answer of <a href="https://stackoverflow.com/users/1109425/nandeesh">nandeesh</a> for further details why it didn't worked before.</p> <pre><code>public void startDialog(){ LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.dialog_content, null); builder = new AlertDialog.Builder(context); builder.setTitle("EditText"); builder.setView(layout); builder.setPositiveButton("Ok", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { dismiss(); } }); AlertDialog aDialog = builder.create(); aDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); aDialog.show(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload