DialogFragment width shrink after specifying FEATURE_NO_TITLE

Viewed 4069

So here is my onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  View view = inflater.inflate(R.layout.my_view, container, false);
  ...
  return view;
}

And the layout itself is a LinearLayout with

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

For some reason, after adding the line getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); the dialog shrunk to wrap the contents; in fact, one of the TextViews is forced to wrap its one line into two lines. I want the Dialog to fill the width of the screen, save some margin that I specify.

4 Answers

You can also set your width in the root layout, probably you should know that the width of any window in android devices is about 360dp, so if you set the width to 344dp, you will have 16dp of space to play around with your dialog, additionally you should set your layout_gravity to center_horizontal so it will have the usual look, this will do trick.

Example:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="344dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="80dp"></FrameLayout>

A FrameLayout in my case, yours can be anything you need.

Related