Android AlertDialog custom view is cut off

Viewed 27

In my game I use AlertDialog with custom view. On most devices, the dialogs look correct, but on some devices (Samsung Galaxy A73 5G Android 12) they appear cut off. I have attached screenshots for better understanding. I would appreciate any help in solving this problem.

Correct dialog

Cut off

AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.CustomDialogStyle);
View view = activity.getLayoutInflater().inflate(R.layout.message_dialog, null);

String messageText = String.format(Strings.daily_star_bonus_message, Configs.DAILY_BONUS_TIPS);

TextView title = view.findViewById(R.id.dialog_title);
title.setText("Тестовый Диалог 1");

TextView message = view.findViewById(R.id.message_text);
message.setText(Html.fromHtml(messageText));

builder.setCancelable(true);
builder.setView(view);

builder.setPositiveButton(Strings.button_ok, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        dialogInterface.dismiss();
    }
});

builder.show();

message_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
    android:id="@+id/dialog_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="30dp"
    android:paddingTop="14dp"
    android:paddingBottom="9dp"
    android:textColor="@color/black"
    android:textSize="20sp"
    android:textStyle="bold" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="@color/dark_blue" />

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:textColor="@color/grey_text_color"
        android:textSize="16sp" />
</ScrollView>

styles.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Theme.AppCompat.NoActionBar android:Theme.Light.NoTitleBar.Fullscreen-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>

<style name="DictionaryTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowFullscreen">true</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>

<style name="CustomDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/dark_blue</item>
</style>
1 Answers

Will I think the problem is AlertDialog not taking the full height & width of your screen

so can you try this code ?

// Initializing:
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
// Configuring:
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
layoutParams.height (alertDialog.getWindow().getAttributes());
// setting width & height to 90% of display
layoutParams.width = (int) (displayMetrics.widthPixels * 0.9f);
layoutParams.height = (int) (displayMetrics.heightPixels * 0.9f);
// Setting:
alertDialog.getWindow().setAttributes(layoutParams);

also you'll need to use your builder.create(); method for creating AlertDialog then you'll use your dialog.show(); instead of builder.show();

& Please tell me if it not works so I can provide more help & Good Luck

Related