ZooS
ZooS

Reputation: 688

DialogFragment with no title shrinks dialog width

The goal is to create a dialog that shows a list of files.

The problem is that once I remove the title (by any means including window feature, setting style, using styles.xml...) the dialog window converts to wrap_content constraint.

No property I set in layout influences this.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

    <TextView
        android:id="@+id/dialog_files_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:padding="8dp"
        android:text="@string/dialog_files_title"
        android:textColor="@color/colorTextActive"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Title" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/dialog_files_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp" />

</LinearLayout>

my java code

public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = super.onCreateDialog(savedInstanceState);

        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.dialog_files, null);

        paths = new ArrayList<>();
        recursiveScan(Environment.getExternalStorageDirectory());
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dialog_files_recycler_view);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
        AdapterFiles adapter = new AdapterFiles(getActivity(), paths);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        return view;

    }

The result is this

dialog

Upvotes: 5

Views: 1949

Answers (2)

ZooS
ZooS

Reputation: 688

The issue seems to be when using LinearLayout as a root layout. I switched to RelativeLayout everything fit into place. Maybe this is a bug when using LinearLayout as a root layout in dialogs and removing title?

Upvotes: 17

skynet
skynet

Reputation: 726

Dialog dialog = super.onCreateDialog(savedInstanceState);

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

Window window = dialog.getWindow();

if (window != null) {
      window.requestFeature(Window.FEATURE_NO_TITLE);
      window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
}

return dialog;

Try this out.

Upvotes: 0

Related Questions