0

The text in my dialog is too long, and it mixes with buttons. So I figured out I should change the size of the dialog.

upd.: To make it clear, the question is if it's possible to change the size of the alert dialog in such way so that these changes apply to a layout, and how? If it is really impossible, why and what are the alternatives.

Here is how I am trying to do it in my code, according to answers in this topic:

    int matchParent = WindowManager.LayoutParams.MATCH_PARENT;
    rulesDialog.show();
    rulesDialog.getWindow().setLayout(matchParent, matchParent);

And I got this, it just adds the white gap:

enter image description here

Also tried this. That didn't work at all. I also tried to add this line with the same approach, did nothing:

<item name="android:layout_height">match_parent</item>

Here is xml code of a layout passed to the AlertDialog.Builder:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/beige">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:padding="4dp"
        android:layout_margin="4dp"
        android:textColor="@color/brown"
        android:textSize="14sp"
        android:text="@string/info_text"
        android:id="@+id/info_text"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/info_text"
        android:layout_centerHorizontal="true"
        android:text="@string/ok"
        android:padding="4dp"
        android:textSize="14sp"
        android:textColor="@color/orange"
        android:background="@color/blank"
        android:minHeight="0dp"
        android:minWidth="0dp"
        android:layout_margin="4dp"
        android:id="@+id/ok"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/info_text"
        android:layout_toEndOf="@id/ok"
        android:text="@string/renew_record"
        android:padding="4dp"
        android:textSize="14sp"
        android:textColor="@color/orange"
        android:background="@color/blank"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:layout_margin="4dp"
        android:id="@+id/renew_record"/>

</RelativeLayout>

upd.: I've found that this code works:

rulesDialog.show();
int matchParent = WindowManager.LayoutParams.MATCH_PARENT;
rulesDialog.getWindow().setLayout(matchParent, matchParent);
rulesDialog.setContentView(R.layout.info);

Solves the white gap problem totally. For some case, the problem with overlapping still exists. As far as I understand, that's a common problem for RelativeLayout, now I am investigating it.

upd.: Yes, I should've deleted android:layout_centerVertical="true" line from the TextView. Now all works fine with two latter fixes, problem solved.

Rawicz
  • 39
  • 8

1 Answers1

0

You can either use scroll view in your .xml or enable scrolling in textview itself. I think the later is more simple and as follows.

<TextView
        android:id="@+id/info_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ....  
        android:maxLines="7"   <-- play with this number to best suit your expectations-->
        android:scrollbars="vertical"
        />

Finally in your .java/.kt file.

textView.setMovementMethod(new ScrollingMovementMethod());
mohammed ahmed
  • 195
  • 1
  • 10
  • That may be a solution. However, I'd like to know if it's possible to change the size of the AlertDialog properly for the future use. – Rawicz Mar 16 '21 at 14:36
  • Hm, strange, I've tried this, and the buttons somehow still overlap the text, even though the size of the dialog became smaller. – Rawicz Mar 16 '21 at 14:47
  • Did you tired setting height = wrap_content for the root element (Relative layout) to resize your dialog box based on you size of text. – mohammed ahmed Mar 16 '21 at 14:55
  • Change relative to constraint layout / linear layout , it might help with overlapping issue. – mohammed ahmed Mar 16 '21 at 14:57
  • setting height = wrap_content wouldn't work because AlertDialog dumps the layout parameters of the layout root, that's why we usually pass null as a parent when inflating the layout. – Rawicz Mar 16 '21 at 15:05