Saturday, 9 July 2022

Android Dialog cropping problem on Android 12 devices

We have a number of Apps on the market that use DialogFragment (the AppCompatDialogFragment version). We are finding that on certain devices, Samsung TAB S7 for one, dialogs are truncated so that information and action buttons at the bottom are not displayed. Nothing I have tries will resolve this issue in a way that is consistent across all devices. I have produces a simple layout and code to demonstrate. This code when inserted in to the live app displays as per the truncated image, in a newly created test app it displays normally on the same device! All theming, manifest settings appear similar. I have spent many days on this so any suggestions would be much appreciated.

package com.example.dialogclipping;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialog;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.DialogFragment;

public class DlgTest extends AppCompatDialogFragment {



    static DlgTest newInstance() {

        return new DlgTest();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);

    }

    /*
     * (non-Javadoc)
     *
     * @see android.app.DialogFragment#onCreateDialog(android.os.Bundle)
     */
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        var dialog = (AppCompatDialog) super.onCreateDialog(savedInstanceState);
        dialog.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        int targetWidth = 1000;
        int targetHeight = 0;

        // check if specific size setting is required
        if (targetWidth != 0 || targetHeight != 0) {

            var dialog = getDialog();
            if (dialog == null) return;
            var window = dialog.getWindow();
            if (window == null) return;

            var lp = window.getAttributes();
            // overwrite dimensions as appropriate
            if (targetWidth > 0) lp.width = targetWidth;
            if (targetHeight > 0) lp.height = targetHeight;
            window.setAttributes(lp);
        }

    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dlg_test_layout, container, false);
        return view;
    }
    int index = 2;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.setBackgroundColor(0xFF92580C);
        final var closeButton = view.findViewById(R.id.yesButton);
        closeButton.setOnClickListener(v -> {
            dismiss();
        });
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/SettingsDialog"
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                android:orientation="vertical"
                xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/header"
        style="@style/DialogText.Heading"
        android:minEms="16"
        android:layout_gravity="left"
        android:text="Header line"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:background="@color/blue"/>

    <LinearLayout
        android:id="@+id/ButtonBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="right">

        <Button
            android:id="@+id/yesButton"
            style="@style/DialogButton.Large"
            android:textSize="20dp"
            android:layout_width="0dip"
            android:layout_weight="2"
            android:text="Close"
            android:visibility="visible"/>

    </LinearLayout>

    <View
        android:id="@+id/bottomView"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/blue">

    </View>
</LinearLayout>

Clipped image

enter image description here



from Android Dialog cropping problem on Android 12 devices

No comments:

Post a Comment