I always create custom dialog without title to make it centered (both vertical and horizontal) using android:windowNoTitle
in styles.xml
or requestWindowFeature(Window.FEATURE_NO_TITLE)
but some of my dialogs are not center horizontal, for example this dialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="20dp"
android:gravity="center"
android:background="@drawable/dialog_bg" >
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/loading_s"/>
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:text="@string/loading"
android:textColor="@color/dialog_text"
android:textSize="@dimen/dialog_title_text_size" />
</LinearLayout>
This is how to create dialog:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
View v = LayoutInflater.from(getActivity()).inflate(R.layout.dlg_progress, null);
Dialog dlg = new Dialog(getActivity(), R.style.My_Dialog_Style); //My_Dialog_Style contains android:windowNoTitle = true
dlg.setContentView(v);
dlg.setCanceledOnTouchOutside(false);
dlg.setCancelable(true);
return dlg;
}
And here is how it appears on screen
If I remove android:windowNoTitle
attribute this dialog show correctly so the problem only occurs when using dialog without title.
Does anyone know why this happen and how to make dialog always center on screen?