2

I have a custom dialog with elements inside. There is a title on top, a scrollview that has a textview inside (this is where the text goes) and two buttons underneath. What i am trying to achieve here is to expand the scrollview (and the dialog along with it) to fit the text that is going to be presented, and when the dialog reaches up to a certain limit (for example 500dp height) the expansion will stop and any extra text that is not showing, the user will have to just scroll to see it.

I have tried wrap_content but it expands up to a certain point and then it breaks the constraints from the bottom and the scrollview goes under the buttons.

constraint dimens:

android:layout_width="500dp" android:layout_height="500dp"

title textview properties:

android:id="@+id/title"
android:layout_width="500dp"
        android:layout_height="wrap_content"
android:layout_marginTop="40"
        android:paddingStart="20"
        android:text="Title"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"

scrollview properties:

android:id="@+id/scrollview"
android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:paddingStart="20dp"
        android:paddingEnd="20dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toTopOf="@+id/cancel"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title"

textview inside scrollview:

android:id="@+id/text"
android:layout_width="match_parent"
            android:layout_height="wrap_content"

cancel button:

 android:id="@+id/cancel" 
android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_marginStart="100dp"
        android:layout_marginBottom="40dp"
android:text="cancel"

app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"

confirm button:

android:layout_width="150dp"
        android:layout_height=50dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="40dp"
android:text="confirm"
app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"

when i show it from java:

View layoutView = getLayoutInflater().inflate(R.layout.dialog_layout,null);
TextView text = layoutView.findViewById(R.id.text);
text.setText("INSERT TEXT HERE TO VIEW DIALOG BEHAVIOUR");

final Dialog dialog = new Dialog(this);
        dialog.setContentView(layoutView);
        dialog.create();
        dialog.show();

Is there something i miss? i tried putting 0dp under the constraint layout for the scrollview but still nothing. plz help

*EDIT: * based on this question Android ScrollView set height for displayed content and this answer right here Android: why is there no maxHeight for a View?

they somehow managed to do it dynamic. HOWEVER this approach is not applicable to a dialog box since by setting the linear layout up to a height, then dialog's size is not dynamic. Is this even possible??

2 Answers2

0

Try this:

Set the maxHeight to the limit you want programmatically:

text.setMaxHeight(your limit);

Or

Set the maxHeight to the scrollView and then the textView to match_parent

afhamu
  • 930
  • 11
  • 17
0

finally, another workaround for this to happen without extending the scrollview class and implementing this behaviour yourself... this post here: Android ScrollView set height for displayed content

provides a check before inflating the dialog and setting the height dynamically. That solves the problem, a bit too much hassle over something seemingly trivial. You must have wrapped your scrollview in a linear layout though, as it is described in my edited description

sv.measure(0, 0);
if (nsv.getMeasuredHeight() > 200) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                      ViewGroup.LayoutParams.MATCH_PARENT, 200);
        sv.setLayoutParams(lp);
}

If there is another approach that is more appropriate i'm all ears.