I have implemented a custom AlertDialog for my app which works as intended with all devices I've tested except in particular for Samsung Devices running on Android 9 using the Samsung One UI. For the said Samsung devices, when I create the Dialog, it always shows up at the bottom of the screen rather than at the center.
My Custom AlertDialog
public static class DialogViewHolder {
private CustomButton btnAccept, btnCancel;
private CustomTextView title, message;
private AlertDialog alertDialog;
private View view;
private Context context;
private LinearLayout bg;
public DialogViewHolder(Context context) {
this.inflateView(context); // Should be the first
this.buildAlertDialog(context);
this.context = context;
}
/**
* Inflates the custom view
*/
private void inflateView(Context context) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
this.view = inflater.inflate(R.layout.custom_alert_dialog, null);
}
/** Builds the alert Dialog */
private void buildAlertDialog(Context context) {
// Create Builder Configuration
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.CustomDialog);
builder.setCancelable(false);
builder.setView(this.view);
}
}
I've already tried playing with my layout files to make sure it wasn't a fluke but then I confirmed that this is only happening specifically on Samsung devices running Android Pie.
Was I correct to assume it had something to do with Samsung One UI? From what I've seen with the UI, most of the dialogs using said UI displays the alert dialog at the bottom of the screen. My end goal here is to be able to keep my custom AlertDialog in the middle of the screen.
Layout of the Custom AlertDialog
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:id="@+id/dialog_bg_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/alert_background_color"
android:orientation="vertical">
<RelativeLayout
android:layout_width="@dimen/logout_container_width"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="@dimen/logout_padding_top"
android:paddingRight="@dimen/logout_padding_left_right"
android:paddingLeft="@dimen/logout_padding_left_right">
<com.example.dialog.custom_views.CustomTextView
android:id="@+id/alert_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/logout_padding_top"
android:gravity="center"
android:textAlignment="center"
android:textColor="@color/alert_title_color"
android:textSize="@dimen/logout_header_title_size"
app:font_trait="Bold" />
<com.example.dialog.custom_views.CustomTextView
android:id="@+id/alert_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/alert_header"
android:gravity="center"
android:paddingBottom="@dimen/data_usage_header_margin"
android:paddingTop="@dimen/data_usage_header_margin"
android:textAlignment="center"
android:textColor="@color/alert_negative_button_color"
android:textSize="@dimen/logout_message_size" />
<com.example.dialog.custom_views.CustomButton
android:id="@+id/positive_button"
android:layout_width="match_parent"
android:layout_height="@dimen/logout_button_height"
android:layout_below="@+id/alert_message"
android:layout_marginBottom="@dimen/dialog_buttons_margin"
android:background="@color/alert_positive_button_color"
android:textSize="@dimen/logout_buttons_text_size"
app:font_trait="Normal"/>
<com.example.dialog.custom_views.CustomButton
android:id="@+id/negative_button"
android:layout_width="match_parent"
android:layout_height="@dimen/logout_button_height"
android:layout_below="@+id/positive_button"
android:background="@color/alert_negative_button_color"
android:textSize="@dimen/logout_buttons_text_size"
android:layout_marginBottom="@dimen/dialog_cancel_margin_bottom"
app:font_trait="Normal">
</com.example.dialog.custom_views.CustomButton>
</RelativeLayout>
</LinearLayout>
</FrameLayout>