3

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?

N J
  • 27,217
  • 13
  • 76
  • 96
Wayne
  • 6,361
  • 10
  • 46
  • 69

4 Answers4

2

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

Community
  • 1
  • 1
reidisaki
  • 1,525
  • 16
  • 29
  • You can see I used `android:layout_gravity="center" ` already. Many of my dialogs use `LinearLayout` as dialog container but it show correctly so I don't think problem is caused by LinearLayout. – Wayne Jul 19 '13 at 18:03
2

When you use Builder and set a custom view with setView, it should not be necessary to remove the Dialog's title and the dialog should be centered.

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.dlg_progress, null));
    return builder.create();
}

This is very similar to how it is done in the docs: Creating a Custom Layout

brillenheini
  • 5,413
  • 3
  • 23
  • 20
  • Thank you but I want to create a fully-customized and complicated dialog, so I need to use Dialog than just AlertDialog.Builder. – Wayne Jul 22 '13 at 04:27
  • @Wayne Then you could use `onCreateView`, like you would with a normal fragment, instead of `onCreateDialog`. To remove the title, use `setStyle(STYLE_NO_TITLE, 0)`. – brillenheini Jul 22 '13 at 09:57
  • @Wayne Sorry, no idea then. – brillenheini Jul 24 '13 at 18:29
1

I believe you're running lower the dialog's minimum width attribute. It can be found as

<item type="dimen" name="dialog_min_width_major">65%</item>

in Android's framework. It varies depending on which values folder you're looking at, so it differs depending on density, orientation, etc.

You may be able to overwrite this value in your style. If you set it to something that is definitely smaller than your dialog(10%), it may work properly. If not, read on.

If you notice in your view tree panel, it shows your LinearLayout nested inside 3 FrameLayouts. My guess is that the deepest FrameLayout has its width set to wrap_content, so it's not filling the parent layout and is only as big as your LinearLayout. I can't be sure, though, because the dimensions are chopped off in your picture.

Why it changes when you remove the title? I don't know. You can hack it by adjusting the padding/layout params in onMeasure, but it seems like there should be a cleaner way to do it.

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • Thank for your answer but `android:windowMinWidthMajor` and dialog min_width still did not help. – Wayne Jul 22 '13 at 03:25
  • Hmm. Is there a reason you're not just using a `Builder` like brillenheini suggested? It's the simplest way I know to make a title-less dialog, and it centers for me just fine that way. – Geobits Jul 22 '13 at 04:16
  • Because I want to create a fully-customized dialog, so I need to use Dialog instead of AlertDialog. Above progress dialog is just an example, I have many complicated dialog than that. – Wayne Jul 22 '13 at 04:23
0

Still don't know why removing title make Dialog not centered horizontally but when I set min_width attr of LinearLayout = dialog minWidth this problem gone away.

N J
  • 27,217
  • 13
  • 76
  • 96
Wayne
  • 6,361
  • 10
  • 46
  • 69