suraj shinde
suraj shinde

Reputation: 230

how to add recycler view on dialog

Im trying to add recycler view on dialog but dialog not showing anything...I have added cards on recycler view and want to display recycler view on dialog

android.support.v7.app.AlertDialog.Builder dialog = new android.support.v7.app.AlertDialog.Builder(getContext());
LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.last_transaction_report, null);
recyclerView = (RecyclerView) dialogView.findViewById(R.id.transactio_rep_recyclerView);
dialog.setView(dialogView);

AlertDialog alertDialog = dialog.create();
// alertDialog.setContentView(dialogView);
alertDialog.show();

adapter = new TransactionReportCardAdapter(listTransactionDetails, this);
recyclerView.setAdapter(adapter);

Upvotes: 4

Views: 18473

Answers (4)

Khashayar Ghamati
Khashayar Ghamati

Reputation: 368

You can create one layout that contains RecyclerView then set it in dialog :

Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.your_layout);
RecyclerView recyclerView = (RecyclerView) dialog.findViewById(your recycler);

Upvotes: 4

P. Huy
P. Huy

Reputation: 81

Try this:

            val dialogBuilder = AlertDialog.Builder(this@MainActivity)
            val rcvDialog = RecyclerView(this@MainActivity)

            rcvDialog.adapter = DialogAdapter(ArrayList<Item>) //DialogAdapter create by your self
            rcvDialog.layoutManager = LinearLayoutManager(this@MainActivity)
            dialogBuilder.setView(rcvDialog)

            val dialog: Dialog = dialogBuilder.create()
            dialog.show()

Upvotes: 1

Android Developer
Android Developer

Reputation: 9653

Assuming R.layout.dialog_layout is a layout with recyclerview

Dialog dialog = new Dialog(context, R.style.DialogSlideAnim);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.setContentView(R.layout.dialog_layout);
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);
dialog.show();

RecyclerView rvTest = (RecyclerView) dialog.findViewById(R.id.rvTest);
rvTest.setHasFixedSize(true);
rvTest.setLayoutManager(new LinearLayoutManager(context));
rvTest.addItemDecoration(new SimpleDividerItemDecoration(context, R.drawable.divider));

DataDialogAdapter rvAdapter = new DataDialogAdapter(context, rvTestList);
rvTest.setAdapter(rvAdapter);

styles.xml

<style name="DialogSlideAnim" parent="@android:style/Theme.Dialog">
    <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style>
<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_up_dialog</item>
    <item name="android:windowExitAnimation">@anim/slide_down_dialog</item>
</style>

Upvotes: 9

Bali
Bali

Reputation: 759

TransactionReportCardAdapter  adapter = new TransactionReportCardAdapter(listTransactionDetails, context);
adapter.setAdapter(themeAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

you should use LayoutManager as it's responsible for measuring and positioning item views within a RecyclerView.

Upvotes: 0

Related Questions