Bhaskar Jyoti Dutta
Bhaskar Jyoti Dutta

Reputation: 1800

How to show recyclerVIew in Dialog?

My XML is as follows- live_test_question_change_dialog_layout.xml

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

    <TextView
        android:id="@+id/live_test_change_question_option_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/a"
        android:background="@drawable/option_number_background"
        android:textStyle="bold"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        />

</RelativeLayout>

ShowDialog method is as follows-

  /*---------------------------------------------------*/

public void showDialog(){

    final Dialog dialog = new Dialog(getApplicationContext(), R.style.FullHeightDialog);
    dialog.setContentView(R.layout.live_test_question_change_dialog_layout);
    dialog.setCancelable(false);

    if (dialog.getWindow() != null){
        dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    }

    RecyclerView recyclerView = dialog.findViewById(R.id.live_test_question_change_dialog_rec_view_id);
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    ArrayList<String> questionNumberList = new ArrayList<>();

    for(int i = 1; i<= testModel.getTotalQuestions(); i++){
        questionNumberList.add(String.valueOf(i));
    }

    CustomAdapter customAdapter = new CustomAdapter(questionNumberList);
    recyclerView.setAdapter(customAdapter);

    dialog.show();

}

/*--------------------------------------------------*/

But the app crash when I try to show to dialog -

    android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView

What is the problem with this code? How to solve this problem?

Upvotes: 1

Views: 871

Answers (2)

Lheonair
Lheonair

Reputation: 498

You've got to create a new 'fragment' which extends from DialogFragment

public class MyDialogWithRecyclerView extends DialogFragment

The official documentation is good and I've used it in order to create a Firestore RecyclerView in a DialogFragment. It's the same for a regular recycler view.

https://developer.android.com/reference/android/app/DialogFragment

If you've worked with RV and fragments you should be fine but you can ask me any questions.

EDIT Sorry, that version is deprecated, this is the correct documentation https://developer.android.com/reference/android/support/v4/app/DialogFragment.html

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69709

I think you are using androidx dependency

You should use

<androidx.recyclerview.widget.RecyclerView

instead of

<android.support.v7.widget.RecyclerView

Upvotes: 2

Related Questions