Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android dialog without title not centered horizontally

Tags:

android

dialog

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

enter image description here

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?

like image 467
Wayne Avatar asked Jan 29 '26 20:01

Wayne


1 Answers

have you tried looking at this thread?

How to align custom dialog centre in android ?

 android:layout_gravity="center" 

It looks like its just a layout change, or try using relativeLayout or LinearLayout instead of FrameLayout

like image 68
reidisaki Avatar answered Jan 31 '26 08:01

reidisaki