Ribal Raza
Ribal Raza

Reputation: 33

Placement of Dialog box in android studio main activity of Android

I want to delete the recycler view item by long press methode and on long press , a dialog box should appear with yes and no. As i click on yes, the item should be delete.. My code for dialog box...

 public static class DialogBox extends DialogFragment  {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        builder.setTitle("Do you want to delete it?").setMessage("Confirmation Dialog")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {


            }
        });
        return builder.create();
    }
}

} My code for delete button of dialog box

 public void onDeleteItem(int position) {

            DialogBox dialogBox=new DialogBox();
            dialogBox.show(getSupportFragmentManager(),"Example Dialog");

            removeItem(position);
        }
    });

Here what should I do that I can easily remove the item by placing the code at right position

Upvotes: 2

Views: 59

Answers (1)

Sujal
Sujal

Reputation: 101

You can use this Alternative. In this scenario You can long press the item and you will get the delete option if you press delete thn it will delete the item else you can press out side of that item to cancel.

 @Override
    public void onBindViewHolder(EmployeeListAdapter.ViewHolder holder, final int position) {


        holder.cardClick.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
                MenuItem delete = contextMenu.add(0, position, 0, "Delete");
                delete.setOnMenuItemClickListener(onDeleteMenu);
            }
        });

    }

     /*
     * Delete Menu
     * */
    private final MenuItem.OnMenuItemClickListener onDeleteMenu = new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            deleteTaskApi(IdList.get(item.getItemId() ));
            return true;
        }
    };

Upvotes: 1

Related Questions