Why is my DialogFragment not showing the custom layout?
Asked Answered
F

6

8

I want to show a DialogFragment with no title, buttons, etc.(i.e, none of what is included in a typical Dialog) but just a ProgressBar spinner on a plain background. For a rough idea, the background's width matches that of the parent and spinner lies at the center of it. For this, I have a custom layout as follows:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <View
        android:id="@+id/search_progress_alpha_indicator"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        android:background="#00ffffff"/>

    <ProgressBar
        android:id="@+id/search_tutors_progress_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:visibility="gone"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        app:layout_constraintGuide_percent="0.20"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="126dp"
        tools:layout_editor_absoluteX="0dp" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline3"
        app:layout_constraintGuide_percent="0.80"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="378dp"
        tools:layout_editor_absoluteX="0dp" />


</android.support.constraint.ConstraintLayout>

Here, View is the background on top of which the ProgressBar is supposed to spin.

But when I inflate this layout in the DialogFragment, it shows something completely different:

enter image description here

Here is the DialogFragment code:

public static class SearchIndicatorDialogFragment extends DialogFragment{

        public static String FRAGMENT_TAG = "searchIndDiaFragTag";

        private View alphaSearchIndicator;
        private ProgressBar progressBar;


        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

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

            View dialogRootView = inflater.inflate(R.layout.fragment_search_dialog, container, false);

            // Obtain the ProgressBar
            progressBar = (ProgressBar) dialogRootView.findViewById(R.id.search_tutors_progress_bar);

            // Obtain the Alpha search indicator
            alphaSearchIndicator = dialogRootView.findViewById(R.id.search_progress_alpha_indicator);

            return dialogRootView;
        }

        @Override
        public void onStop() {

            progressBar.setVisibility(View.GONE);

            alphaSearchIndicator.setBackgroundColor(0x00ffffff);

            super.onStop();
        }

        @Override
        public void onResume() {
            progressBar.setVisibility(View.VISIBLE);

            alphaSearchIndicator.setBackgroundColor(0xA6ffffff);

            super.onResume();
        }

    }

Why is it not showing my custom layout as intended?

EDIT: As an additional note, The problem in my case is that the background View is supposed to be occupying 60% area of screen(height wise) and for this I have constrained it in between the Guidelines. This is something, I am unable to achieve with other answers to similar questions regarding DialogFragment. Moreover, I would like to know, why does a DialogFragment doesn't display the correct by default in general?

Functional answered 6/7, 2017 at 7:31 Comment(2)
#23991226Seemly
@IntelliJAmiya : Saw that. The problem in my case is the background View is supposed to be occupying 50% area of screen(height wise) and for this I have constrained it in between the Guidelines. That linked answer sets match_parent width with LayoutParams easily but how I am supposed to handle something complex as my layout warrants?Functional
A
24

If you change ConstraintLayout to Relative or Linear This problem will be solved.

Ashlaring answered 11/2, 2019 at 10:59 Comment(2)
Wow, I don't understand how this worked. I am using AppCompatDialogFragmentPomeroy
What is the reason for this? I am facing the same problem. I have two different source codes. When I switched to the new source code, it stopped working. :( I tried the same ConstraintLayout with other screen and it worked properly but with DialogFragment, it isn't working in my new source code.Dunbarton
D
8

I was facing the same problem and I put my ConstraintLayout inside RelativeLayout and it worked.

I tried the same with LinearLayout but it didn't work.

Dunbarton answered 7/6, 2019 at 9:40 Comment(0)
R
3

Put ConstraintLayout inside RelativeLayout. The issue will be solved.

Ramrod answered 31/5, 2021 at 10:57 Comment(0)
T
2

Try this:

    AlertDialog builder = new AlertDialog.Builder(this).create();
    builder.setView(progressbar_id);
    Window window = builder.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    builder.show();
Tripura answered 6/7, 2017 at 9:58 Comment(0)
C
1

try this

 Window window = yourDialog.getWindow();
 window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
 window.setGravity(Gravity.CENTER);

or

int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height =  getResources().getDimensionPixelSize(R.dimen.popup_height);        
getDialog().getWindow().setLayout(width, height);
Catacomb answered 6/7, 2017 at 7:33 Comment(2)
@pulp_fiction onResume(), not onCreateView()Catacomb
Okay, I did it in onResume() this time and not showing correctly. The color is not as I intended and height is not what I intended either.Functional
N
0

Try changing

View dialogRootView = inflater.inflate(R.layout.fragment_search_dialog, container, false);

to

 View dialogRootView = inflater.inflate(R.layout.fragment_search_dialog, container, true);

I suppose we are the ones responsible for attaching our layout file’s View to its root ViewGroup.

Nazareth answered 6/7, 2017 at 8:5 Comment(3)
No, it is the exact same output with and without attaching it with trueFunctional
Just did. Nope, no change :(Functional
please also add the code of how are you attaching the dialog fragment to rootViewGroupNazareth

© 2022 - 2024 — McMap. All rights reserved.