java – AlertDialog being trimmed in Android Pie-ThrowExceptions

Exception or error:

I have an app which gets the data from my web server’s database and displays it into an AlertDialog, the app works fine on Android 8 (Oreo) and below but on Android 9 (Pie), the Dialog is being trimmed, essentially hiding the data that was fetched.

I have tried using the AlertDialogBuilder but still the same issue, I’m new to Android development and would appreciate a detailed explanation.

public class BackgroundWorker extends AsyncTask<String, Void, String> {
    Context context;
    AlertDialog alertDialog;
    BackgroundWorker (Context ctx) {
        context = ctx;
    }
    @Override
    protected String doInBackground(String... strings) {
        String destination = strings[0];
        String query_url = "http://prateekrawat.000webhostapp.com/query.php";
        try {
            URL url = new URL(query_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data = URLEncoder.encode("destination", "UTF-8") + "=" + URLEncoder.encode(destination, "UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();


            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

            String line = "";
            String result = "";
            while ((line = bufferedReader.readLine()) != null)
                result = result + "\n" + line;
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Buses");
    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="395dp"
        android:layout_height="717dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="80dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:gravity="center"
            android:text="Enter Destination"
            app:layout_constraintBottom_toTopOf="@+id/editText3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.846" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="100dp"
            android:layout_marginTop="50dp"
            android:layout_marginEnd="8dp"
            android:ems="10"
            android:gravity="center"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.496"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="214dp" />

        <Button
            android:id="@+id/button4"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="177dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:layout_marginTop="80dp"
            android:onClick="Submit"
            android:text="Submit"
            android:textSize="20sp" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Demo Images

Android Pie
Android Oreo

AlertDialog on Pie

AlertDialog on Oreo

How to solve:

This is due to style, please create custom style and put there it will work fine.

 <style name="MyAlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
   <item name="colorAccent">@color/colorAccent</item>
   <item name="android:colorBackground">@color/alertDialogBackground</item>
   <item name="android:windowBackground">@color/alertDialogBackground</item>
 </style>

 AlertDialog.Builder builder = new AlertDialog.Builder(
                getActivity(), R.style.MyAlertDialogTheme);

###

try creating custom Dialog Style and add with and height in percentage.

min_with_minor is:

The platform’s desired minimum size for a dialog’s width when it is
along the minor axis (that is the screen is portrait). This may be
either a fraction or a dimension.

Min_witdh_major is:

The platform’s desired minimum size for a dialog’s width when it is
along the minor axis (that is the screen is portrait). This may be
either a fraction or a dimension.

try below Example.

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">

    <item name="android:windowMinWidthMajor">80%</item>
    <item name="android:windowMinWidthMinor">80%</item>
    <item name="windowFixedHeightMinor">80%</item>
    <item name="windowFixedHeightMajor">80%</item>
    <item name="android:windowBackground">@drawable/ubp_bg_white_rect</item>
    <item name="colorAccent">#FFC107</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">#FFFFFF</item>
    <!-- Used for the background -->
    <item name="android:background">#4CAF50</item>

</style>


 AlertDialog.Builder myAlert = new AlertDialog.Builder(this, R.style.AlertDialogCustom);
        myAlert.setTitle("Title");
        myAlert.setMessage("1298201");
        myAlert.show();

Below is Sample Result for Oreo :

Oreo Result

Below is sample result for Pie;

Pie Result

Leave a Reply

Your email address will not be published. Required fields are marked *