KevinBurton
KevinBurton

Reputation: 51

Android AlertDialog text is cut off at bottom

I want to display both an image and a longer description to the user using an AlertDialog, where the image is static and the text can be scrolled. Using a ScrollView the text can be scrolled but only to a certain extent, since it is cut off at the bottom. Below is my Dialog code and the xml Layout and a screenshot showing the issue.
The Dialog is called using the constructor, passing the image, text and title to be displayed.

public class LocationDialog extends AppCompatDialogFragment {

    private String title;
    private Bitmap image;
    private String text;

    public LocationDialog(String title, Bitmap image, String text) {
        this.title = title;
        this.image = image;
        this.text = text;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_layout, null);

        TextView text =  view.findViewById(R.id.textViewMain);
        text.setMovementMethod(new ScrollingMovementMethod());
        text.setText(this.text);

        ImageView image = view.findViewById(R.id.imageViewMain);
        image.setImageBitmap(this.image);

        builder.setView(view)
                .setTitle(title)
                .setPositiveButton(R.string.pos_button_alert_location, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        return builder.create();
    }
}

Layout:

<ImageView
        android:id="@+id/imageViewMain"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:padding="0dp"
        android:scaleType="fitXY"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageViewMain">

        <TextView
            android:id="@+id/textViewMain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:scrollbars="vertical"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageViewMain" />

    </ScrollView>

Screenshot

EDIT: I have now tried to do a similar functionality with a PopupWindow but the exact same problem happens. Using example texts I was able to ascertain that it cuts off about 16% of the text. Help would be very much appreciated.

Upvotes: 1

Views: 1622

Answers (1)

KevinBurton
KevinBurton

Reputation: 51

I was able to solve the issue by using a LinearLayout instead of a ConstraintLayout as my top level layout. The layout widths and heights all stayed the same which leads me to believe, that it is a bug specific to ConstraintLayouts and AlertDialogs.

Upvotes: 4

Related Questions