Change the background of a button in MainActivity

I have created a class that extends DialogFragment. In that class, when I press a button, I want to change the background of a button that is placed in MainActivity.

I have tried to create an inner class to gain access to MainActivity objects but what I get is this error:

Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference


@SuppressLint("ValidFragment")
public class Popup extends DialogFragment {
    private final MediaPlayer _mediaPlayer = new MediaPlayer();
    private final int _layout;
    private TextInputEditText _customTextField;
    private final Dialog mDialog = new Dialog();
    public boolean dialogIsActive = false;

    @SuppressLint("ValidFragment")
    public Popup(int layout) {
        _layout = layout;
    }

    public interface ICustomTts {
        void customTts(String input, Activity activity);
    }
    public ICustomTts iCustomTts;

    public interface  ITarget {
        String getTarget(String input);
    }
    public ITarget iTarget;

    @SuppressLint({"ClickableViewAccessibility", "ResourceType"})
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        final View view =  inflater.inflate(_layout, container, false);

        // Display fragment_dialog
        if (_layout == R.layout.fragment_dialog) {
            // Read and display the expressions
            view.findViewById(R.id.dialogHelp).setOnClickListener(v -> {
                String expressions = null;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    expressions = Helper.readFile(getContext(), "expressions.yml");
                }
                TextView models = view.findViewById(R.id.dialogModels);
                models.setText(expressions);
            });
            // Toggle the listener
            Button cta = view.findViewById(R.id.dialogCta);

            // Gain access to MainActivity
            class InnerMainActivity extends my.app.MainActivity {
                @RequiresApi(api = Build.VERSION_CODES.M)
                public void dialog(boolean active) {
                    int button = (active) ? R.drawable.button_red : R.drawable.button_yellow;
                    findViewById(R.id.dialog).setBackground(ContextCompat.getDrawable(getContext(), button));
                }
            }
            InnerMainActivity innerMainActivity = new InnerMainActivity();

            cta.setOnClickListener(v -> {
                if (!dialogIsActive) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (mDialog.hasRecordPermission(getContext())) {
                            mDialog.startService(getContext());
                            cta.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.button_red));
                            cta.setText(R.string.dialog_do_not_listen);
                            cta.setTextSize(17);
                            innerMainActivity.dialog(true);
                            dialogIsActive = true;
                        }
                    }
                }
                else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        mDialog.stopTheService(getContext());
                        cta.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.button_yellow));
                        cta.setText(R.string.dialog_listen);
                        innerMainActivity.dialog(false);
                        dialogIsActive = false;
                    }
                }
            });
        }
                
        return view;
    }      

}

Solution 1:

You can use interface to do this. in your DialogFragment :

public class MyDialog extends DialogFragment {

public boolean dialogIsActive = false;

public interface OnButtonClick {
    void onButtonClick(boolean active);
}

private OnButtonClick onButtonClick;

public MyDialog(OnButtonClick onButtonClick) {
    this.onButtonClick = onButtonClick;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    final View view = inflater.inflate(_layout, container, false);
    Button cta = view.findViewById(R.id.dialogCta);
    cta.setOnClickListener(new View.OnClickListener() {
       @Override
        public void onClick(View v) {
            //onButtonClick.onButtonClick(boolean)   this is where your activity trigger
            if (!dialogIsActive) {
                onButtonClick.onButtonClick(true);
                dialogIsActive = true;
            } else {
                onButtonClick.onButtonClick(false);
                dialogIsActive = false;
            }
        }
    });

    return view;
}

}

in your Activity

public class MyActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyDialog myDialog= new MyDialog(new MyDialog.OnButtonClick(){
        @Override
        public void onButtonClick(boolean active) {
            //this is where you know that the dialog button clicked
            Button activityButton = findViewById(R.id.activityButton);
            int button = (active) ? R.drawable.button_red : R.drawable.button_yellow;
            activityButton.setBackground(ContextCompat.getDrawable(MyActivity.this, button));

        }
    });
    myDialog.show(getSupportFragmentManager(),"tag");
}

}