0

I have a bit of weird situation - I have a made a custom dialog that opens when a button from the activity is clicked. The custom dialog contains a close button that when clicked closes the dialog and returns user to the activity. When I run the code as it is (shown below), instead of launching the custom dialog, it goes to the main activity (I think either reopens the app or somehow creates a imaginary intent for the main activity). However, when I remove/comment the code for the close button - highlighted as // === this code, everything works fine (the custom dialog opens) but the close button doesn't function. I am not sure what I am missing.

    // Global variable
    Button openDialog;

    // ======
    openDialog = (Button) findViewById(R.id.opendialog);
    openDialog.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {

            final Dialog cusomDialog = new Dialog(sellActivity.this);
            cusomDialog.setContentView(R.layout.customdialog);


            // === This code
            final Button close = (Button) findViewById(R.id.close);
            close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cusomDialog.dismiss();

                    }
                });
            // === This code

            cusomDialog.show();


        }
    });

Can someone please shed some light on this, as I am really confused.

SumOne
  • 817
  • 3
  • 13
  • 24

4 Answers4

4

Initialize your View of Dialog inside Dialog:

Replace this:

final Button close = (Button) findViewById(R.id.close);

With this:

final Button close = (Button) cusomDialog.findViewById(R.id.close);
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
3

At First, You should pass cusomDialog View Object .

Secondly , Dialog Create and call method wrong .

   final Button close = (Button)cusomDialog.findViewById(R.id.close);
        close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cusomDialog.dismiss();

                }
            });

Same problem

openDialog = (Button)cusomDialog.findViewById(R.id.opendialog); //Rectified

You should read Custom Dialog for more information .

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

I think you should inflate the layout and get the view and set the view in your custom dialog.

final Dialog cusomDialog = new Dialog(sellActivity.this);
View view = LayoutInflater.fromContext(sellActivity.this).inflate(R.layout.customdialog,false);
cusomDialog.setContentView(view);
Button close = (Button) view.findViewById(R.id.close);

Good Luck!!

divyansh ingle
  • 340
  • 1
  • 9
0
  1. If you use DataBinding write a callback in XML:

    android:onClick="@{_ -> viewModel.onClickAction()}"
    
  2. If you use ViewBinding make sure onCreateView and onViewCreated are called. See Android DialogFragment onViewCreated not called.

    private lateinit var binding: YourDialogBinding
    
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        binding = YourDialogBinding.inflate(layoutInflater)
        // Create usual or custom dialog
        val builder = AlertDialog.Builder(requireContext()).setView(binding.root)
        return builder.create()
    }
    
    // Need to return the view here or onViewCreated won't be called by DialogFragment
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        super.onCreateView(inflater, container, savedInstanceState)
        return binding.root
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        with(binding) {
            ...
    
CoolMind
  • 26,736
  • 15
  • 188
  • 224