android-studioandroid-buttonandroid-dialog

Changing button attributes inside a dialog box upon button click


I have a dialog box that opens upon clicking a button. Inside the dialog box contains multiple buttons, but for the sake of simplicity, I will only focus on the "Sun" button. The ideal outcome of the button is that if the button is pressed, the text color of "Sun" will turn into green, then if the button is pressed again, it will turn back into its normal white text color. This image is the dialog box

Listed below are the source codes for context.

Button Attributes XML

<Button
                    android:id="@+id/sunday_btn"
                    android:layout_width="90sp"
                    android:layout_height="90sp"
                    android:layout_gravity="top|left"
                    android:background="@drawable/roundstyle_darkgray"

                    android:fontFamily="@font/inria_sans_bold"
                    android:text="Sun"
                    android:textAllCaps="false"
                    android:textColor="@color/white"
                    android:textSize="30sp"
                    android:onClick="sundayToggle"/>

colors.xml

\<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="lightGray">#D9D9D9</color>
    <color name="darkGray">#404040</color>
    <color name="darkerGray">#141414</color>
    <color name="selectedGreen">#57E44B</color>
</resources>

RateList.java

public class RateList extends AppCompatActivity {
    private boolean bool_editMode = false;
    private final boolean[] popup_selectedWdBool = {false,false,false,false,false,false,false};

    Dialog dialog;
    Button btnDialogSave, btnDialogCancel, sundayBTN;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_rate_list);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        ((Button)findViewById(R.id.add_constant_btn)).setVisibility(View.INVISIBLE);

        dialog = new Dialog(RateList.this);
        dialog.setContentView(R.layout.add_constant_popup);
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setCancelable(false);

        btnDialogCancel = (Button)findViewById(R.id.cancel_button);
        btnDialogSave = (Button)findViewById(R.id.save_button);
        sundayBTN = (Button)findViewById(R.id.sunday_btn);
    }

    public void onClickSave(View view) {
        dialog.dismiss();
        Toast.makeText(RateList.this, "Saved", Toast.LENGTH_SHORT).show();
        // SAVE INFO TO CSV
    }

    public void onClickCancel(View view) {
        dialog.dismiss();
        Toast.makeText(RateList.this, "Cancelled", Toast.LENGTH_SHORT).show();
    }

    public void sundayToggle(View view) {
        if (!popup_selectedWdBool[0]){
            Toast.makeText(RateList.this, "Sunday Selected", Toast.LENGTH_SHORT).show();
            sundayBTN.setTextColor(Color.parseColor("#57E44B"));
        }else{
            Toast.makeText(RateList.this, "Sunday Deselected", Toast.LENGTH_SHORT).show();
            sundayBTN.setTextColor(Color.parseColor("#FFFFFF"));
        }
        popup_selectedWdBool[0] = !popup_selectedWdBool[0];
    }
}

With the code provided, this is where my problem lies. Under sundayToggle(), there is an if-else statement that handles the selected and unselected part of the button. All of those work except these lines: sundayBTN.setTextColor(Color.parseColor("#57E44B")); and sundayBTN.setTextColor(Color.parseColor("#FFFFFF"));. I tested these by having both of these lines commented out, which made the program work and the Toast worked. But if I uncommented them, not just the dialog box but the whole activity crashes when I press it. I am now wondering what I did wrong since everything went well up to this part. My save and cancel works for additional context.

I directly used (Button)findViewById(R.id.sunday_btn); before turning it into a variable named sundayBTN. Both didn't work.


Solution

  • NEVER MIND I FOUND THE ANSWER Apparently you have to specify that the button you are trying to change is from the dialog box, not from the layout itself.

    So from this: sundayBTN = (Button)findViewById(R.id.sunday_btn);

    I turned it to this: sundayBTN = (Button)dialog.findViewById(R.id.sunday_btn);

    Which made the whole thing work!