Reputation: 6449
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?
Upvotes: 3
Views: 2159
Reputation: 6449
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.
Upvotes: 0
Reputation: 22342
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.
Upvotes: 1
Reputation: 5453
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
Upvotes: 2
Reputation: 1534
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
Upvotes: 2