skip to Main Content

In my code, dialog.dismiss(); function not working but callback function working perfectly, callback.onItemClicked(position);

Please help me, I’m new in the android field!

The same code working fine in activity, but problems arrive in the adapter class.

Adapter Class

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
    private final Context mContext;
    private final ArrayList<String> mData;
    AdapterCallback callback;
    Dialog dialog;


    public Adapter(Context mContext, ArrayList<String> mData, AdapterCallback callback) {
        this.mContext = mContext;
        this.mData = mData;
        this.callback = callback;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        if (Helper.readSharedPref(mContext, Constants.PREF_KEY_Sub).equalsIgnoreCase("notActive")) {
            if (!mData.get(position).equalsIgnoreCase("PENDING")) {
           alertSuccessShowDialog(mContext, position);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public interface AdapterCallback {
        void onItemClicked(int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_bank_name;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_bank_name = itemView.findViewById(R.id.tv_bank_name);
        }
    }


    private void alertSuccessShowDialog(Context context, int position) {
        dialog = new Dialog(context);
        dialog.setContentView(R.layout.enquiry_popup);
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.getWindow().setGravity(Gravity.CENTER);
        dialog.setCancelable(false);
        dialog.findViewById(R.id.iv_close_btn).setOnClickListener(v -> {
            dialog.dismiss();
            callback.onItemClicked(position);
        });

        dialog.show();
    }

}

3

Answers


  1. You have to change your code. Because you can’t dismiss the dialog before showing it. So change your code like this.

    private void alertSuccessShowDialog(Context context, int position) {
            dialog = new Dialog(context);
            dialog.setContentView(R.layout.enquiry_popup);
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            dialog.getWindow().setGravity(Gravity.CENTER);
            dialog.setCancelable(false);
            dialog.show();
            
            dialog.findViewById(R.id.iv_close_btn).setOnClickListener(v -> {
                dialog.dismiss();
                callback.onItemClicked(position);
            });
    }
    
    Login or Signup to reply.
  2. What @Dev4life said is right you have to call after create the dialog, and you should use AlertDialog instead of Dialog because of security reasons i believe. There is a brief statement in the Documentation about:
    Dialogs

    The Dialog class is the base class for dialogs, but you should avoid
    instantiating Dialog directly. Instead, use one of the following
    subclasses:

    AlertDialog A dialog that can show a title, up to three buttons, a
    list of selectable items, or a custom layout. DatePickerDialog or
    TimePickerDialog A dialog with a pre-defined UI that allows the user
    to select a date or time.

    Here is my code dunno, but should work:

    public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
        
        private final Context mContext;
        private final ArrayList<String> mData;
        AdapterCallback callback;
        
        AlertDialog dialog;
        Button btnClose;
        AlertDialog.Builder builder;
    
        public Adapter(Context mContext, ArrayList<String> mData, AdapterCallback callback) {
            this.mContext = mContext;
            this.mData = mData;
            this.callback = callback;
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            
            TextView tv_bank_name;
    
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                tv_bank_name = itemView.findViewById(R.id.tv_bank_name);
            }
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item, parent, false));
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
            if (Helper.readSharedPref(mContext, Constants.PREF_KEY_Sub).equalsIgnoreCase("notactive")) {
                if (!mData.get(position).equalsIgnoreCase("pending")) {
               alertSuccessShowDialog(position);
                }
            }
        }
    
        @Override
        public int getItemCount() {
            return mData != null ? mData.size() : 0;
        }
    
        @Override
        public int getItemViewType(int position) {
            return position;
        }
    
        public interface AdapterCallback {
            void onItemClicked(int position);
        }
    
        private void alertSuccessShowDialog(int position) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.enquiry_popup, null, false);
            btnClose = view.findViewById(R.id.iv_close_btn);
    
            builder = new AlertDialog.Builder(mContext);
            builder.setView(view);
            
            dialog = builder.create();
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            btnClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callback.onItemClicked(position);
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
    }
    

    This is my last bet, dunno know how to fix.
    Create a class for your dialog and if needed pass the Context in the Constructor.

    Dialog Class:

    public class DialogEnquiry extends DialogFragment {
            
            private AdapterCallback callback;
            private int position;
    
            public DialogEnquiry(AdapterCallback callback,int position){
                this.callback = callback;
                this.position = position;
            }
    
            @NonNull
            @Override
            public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
                
                View view = LayoutInflater.from(requireContext()).inflate(R.layout.enquiry_popup, null);
                btnClose = view.findViewById(R.id.iv_close_btn);
                btnClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        callback.onItemClicked(position);
                        dismiss();
                    }
                });
    
                return new AlertDialog.Builder(requireContext())
                .setView(view)
                .create();
            }
    
            @Override
            public void onStart() {
            super.onStart();
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            }
        }
    

    And inside of your Adapter, maybe you will need to pass the fragment manager in the adapter, but i dunno if this is right, i’m new in android:

    new DialogEnquiry(callback, position).show(fragmentManager, "dialog");

    This is my last attempt, i hope this can give you some insight.
    Good Lucky!

    Login or Signup to reply.
  3. Nothing wrong with your dialog box method code, just change calling method like this with new context…

    alertSuccessShowDialog(holder.itemView.getContext(), position);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search