Search code examples
javaandroiddialogvisibilitycustomdialog

How to change visibility of a view in custom dialog after showing it android


Want to change visibility of linearlayout by clicking something - after showing the dialog. Sure about clicking method is true. Even it doesn't work without clicking method. Is it possible to do? Example:

    singleA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            linearTYT.setVisibility(View.VISIBLE)

                        }else{

linearTYT.setVisibility(View.GONE)
                        }

                    }
});

It is in dialog code.


Solution

  • Use LayoutInflator you might want to override AlertDialog's Button because it will dismiss() by default onClick()

        View view = getLayoutInflater().inflate(R.layout.your_inflated_layout, null);
    
        final LinearLayout linearLayout = view.findViewById(R.id.your_linear);
        // Now you can do whatever you want with LinearLayout
    
        Dialog.OnShowListener showListener = new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
    
                View.OnClickListener hideListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        linearLayout.setVisibility(View.INVISIBLE);
    
                    }
                };
    
                View.OnClickListener unHideListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        linearLayout.setVisibility(View.VISIBLE);
    
                        // Hiding LinearLayout but this doesn't
                        // dismiss the dialog if you want
                        // call dialogInterface.dismiss();
                    }
                };
    
                // Overriding AlertDialog's Button because we don't want it
                // to dismiss in every click
                Button hide = ((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE);
                Button unhide = ((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_NEGATIVE);
                hide.setOnClickListener(hideListener);
                unhide.setOnClickListener(unHideListener);
            }
        };
    
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setView(view)
                .setPositiveButton("Hide", null)
                .setNegativeButton("Unhide", null);
    
        AlertDialog dialog = builder.create();
        dialog.setOnShowListener(showListener);
        dialog.show();