I was just trying to migrate my application from a Realme to an Oppo and everything looked fine until I noticed the displayed dialogs were not centered. Here is the example (I will only talk about the loading dialog but is a common problem to all dialogs):
Not centered dialog
My code for creating the Dialog
is:
public class GenericAlert extends DialogFragment {
public static final int LOADING_ALERT = 0;
public static final int SENDING_ALERT = 1;
public static final int ERROR_ALERT = 2;
private int alertType;
private boolean showing = false;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
int layoutToSet;
switch (alertType) {
case LOADING_ALERT: layoutToSet = R.layout.loading_alert_layout; break;
case SENDING_ALERT: layoutToSet = R.layout.sending_alert_layout; break;
case ERROR_ALERT: layoutToSet = R.layout.error_alert_layout; break;
default: layoutToSet = R.layout.error_alert_layout; break;
}
View loadingAlertLayout = getLayoutInflater().inflate(layoutToSet, null);
builder.setView(loadingAlertLayout);
if (alertType == SENDING_ALERT) {
builder.setCancelable(false);
}
AlertDialog alert = builder.create();
alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
return alert;
}
...
}
I disabled the transparent background to see how much space was the dialog using: Not centered dialog with white background
Here is my XML file loading_alert_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/less_round_corners_white_borders"
android:padding="10dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<pl.droidsonroids.gif.GifImageView
android:layout_weight=".3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:src="@drawable/loading_animation"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:adjustViewBounds="true"
android:padding="10dp"/>
<androidx.appcompat.widget.AppCompatTextView
android:layout_weight=".7"
android:layout_width="0dip"
android:layout_height="match_parent"
android:text="Cargando"
android:textSize="32sp"
android:textColor="#000"
android:gravity="center_vertical|start"
android:paddingStart="10dp"/>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.AppCompatTextView
android:paddingHorizontal="10dp"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginTop="10dp"
android:text="Se están recibiendo los datos, por favor, espere."/>
</androidx.appcompat.widget.LinearLayoutCompat>
I’m using both physical phones to test my app and I want to get rid of that annoying space.
I’ve tried almost everything that goes through my mind, with no luck, such as setting LayoutParams
when inflating the View, requesting the FEATURE_NO_TITLE
, changing the root view from a linear layout to a relative layout, constraint layout, and so on, and nothing seems to help me delete that empty space, that only occurs on the Oppo phone, not my Realme phone.
Note: When changing the root view, I’ve noticed the spacing does change when using differents views, here are some results I’ve got with views with just 10 dp padding, a red background, and match_parent
and wrap_content
values for layout_width
and layout_height
respectively:
LinearLayoutCompat, ConstraintLayout, AppCompatTextView, AppCompatButton
Seriously, I have no clue why this is happening and I’ve run out of ideas. Also, this is my first question here on StackOverflow, so be free to point out any mistakes I’ve made in my writing.
DeadFrancis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.