• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Is there any way to put a ViewPager2 inside an AlertDialog?

Dnlaua

Lurker
Jul 4, 2018
5
0
I'm losing my mind. Spent over 5 hours rearranging code thinking I was one step away from fixing the issue due to being able to have half the functionality with one parameter change, and the other half of the functionality with a different parameter. I WAS WRONG. NOT CLOSE. I'm hoping you guys can help me.

I'm trying to put a ViewPager2 inside of an AlertDialog that's inside of a RecyclerView. I was going back and forth between having the AlertDialog have the .xml with the ViewPager2 scrolling functionality with the save button not working to dismiss dialog, to having the .xml load, save button work, but no ViewPager2 scrolling. Here's my code for the latter:

editItem()
Code:
{
final AlertDialog.Builder alertDialogBuilder;

inflater = LayoutInflater.from(context);

final View contentEspressoList = inflater.inflate(R.layout.content_espresso_list, null);
//final View view = inflater.inflate(R.layout.activity_espresso_list, null);
final View editItemPageView = inflater.inflate(R.layout.edit_item_page, null);

viewPager2 = (ViewPager2) contentEspressoList.findViewById(R.id.editPager);

alertDialogBuilder = new AlertDialog.Builder(context);

//viewPager2.removeView(viewPager2.getRootView());

alertDialogBuilder.setView(editItemPageView);

final AlertDialog dialog = alertDialogBuilder.create();

viewPager2.setAdapter(new EditViewPagerAdapter(dialog.getContext()));

dialog.show();

saveButton.setOnClickListener(new View.OnClickListener()
{
    [USER=1021285]@override[/USER]
    public void onClick(View v)
    {
        dialog.dismiss();
    }
});
}

EditViewPagerAdapter.java
Code:
public class EditViewPagerAdapter extends RecyclerView.Adapter<EditViewPagerAdapter.ViewHolder>
{
    private String mName;
    private LayoutInflater mInflater;
    private Context ctx;

    public EditViewPagerAdapter(Context context)
    {
        this.ctx = context;
        this.mInflater = LayoutInflater.from(context);
    }

    [USER=1021285]@override[/USER]
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = mInflater.inflate(R.layout.edit_item_page, parent, false);

        return new ViewHolder(view);
    }
 
Last edited:

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones