Almogbb
Almogbb

Reputation: 49

RecyclerView inside AlertDialog is not displaying items

I'm trying to display data in a recyclerView inside a dialog, the data is fetched from an HTTP request.

I verified that the adapter got the data- I printed the array length in the adapter and it was fine,but for some reason it won't display it- all I can see is just a blank white space where the recyclerView should be.

Important note: the app is working fine, no errors on runtime or something, the recyclerView is just not there.

final View dialogView = getLayoutInflater().inflate(R.layout.generate_question_dialog, null);
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setView(dialogView);
alertDialog.show();
Button generateQuestionBtn = alertDialog.findViewById(R.id.generateQuestionBtn);
EditText subjectEditText = alertDialog.findViewById(R.id.subjectEditText);
ProgressBar loading = alertDialog.findViewById(R.id.generateProgressBar);
Objects.requireNonNull(generateQuestionBtn).setOnClickListener(view2 -> {
    if (!TextUtils.isEmpty(subjectEditText.getText().toString())){
        loading.setVisibility(View.VISIBLE);
        HttpUrl.Builder httpBuilder = HttpUrl.parse("ADDRESS").newBuilder();
        OkHttpClient okhttpclient = new OkHttpClient();
        okhttpclient.setReadTimeout(60, TimeUnit.SECONDS);
        httpBuilder.addQueryParameter("subject",subjectEditText.getText().toString());
        Request request = new Request.Builder().url(httpBuilder.build()).build();
        okhttpclient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                System.out.println("FAILURE");
                System.out.println(e.getMessage());
            }

            @Override
            public void onResponse(Response response) throws IOException {
                String[] questions =  response.body().string().split(",");
                loading.setVisibility(View.INVISIBLE);
                QuestionsRecyclerViewAdapter adapter = new QuestionsRecyclerViewAdapter(getContext(),questions);
                new Handler(Looper.getMainLooper()).post(new Runnable(){
                    @Override
                    public void run() {
                        System.out.println("HERE!");
                        RecyclerView questionsRecyclerView = alertDialog.findViewById(R.id.generatedQuestionsRecyclerView);
                        questionsRecyclerView.setAdapter(adapter);
                        questionsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                        questionsRecyclerView.setVisibility(View.VISIBLE);
                    }
                });
            }
        });
    }

xmi file:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:text="@string/enter_a_subject_and_we_ll_find_question_for_you"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/subjectEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="@string/enter_subject"
        android:inputType="textPersonName"
        android:minHeight="48dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/generateQuestionBtn"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20sp"
        android:text="@string/generate_question"
        android:textColor="@color/white"
        android:background="@drawable/button_bg"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/subjectEditText" />

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:paddingStart="185dp"
        android:layout_height="wrap_content"
        android:id="@+id/generatedQuestionsRecyclerView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/generateQuestionBtn" />

    <ProgressBar
        android:id="@+id/generateProgressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:visibility="invisible"
        android:indeterminateTint="@color/blue_p"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/generatedQuestionsRecyclerView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/generateQuestionBtn" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 3

Views: 838

Answers (2)

Shubhendra Singh
Shubhendra Singh

Reputation: 456

You have a added a very large paddingStart (185dp) to your RecyclerView that can be the reason why your view is not visible, please remove this padding and see if this changes any thing.

Upvotes: 0

Ocasin
Ocasin

Reputation: 231

I think there is not enough space inside your dialog to fit the items. The dialog will be created with a size that fits only the empty RecyclerView. When you add the items to the RecyclerView, the content size changes but the dialog doesn't resize to fit the new content size. You can use alertDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); to increase the size of the dialog.

If that doesn't fix it, the problem is problaby located in your QuestionsRecyclerViewAdapter.class or in the way how you display the items.

Upvotes: 2

Related Questions