简体   繁体   中英

Android Studio (Java): Changing Color of a Button directly with java not .xml

I'm programming a little quiz game and I would like to unlock categories. These categories are locked (grey) and should be orange after unlocking them in a dialog. My problem is that that the color doesn't change directly but after closing and opening the activity again. I don't like it because users are confused that the color doesn't change directly. I thought this would be the right approach:

            button_fun.setBackgroundColor(Color.parseColor("#FBBC55"));

Do you know what I'm doing wrong? Thank you!

Edit (More code, tell me if you even need more): Important part from Activity1

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void applyunlock(String kat) {
        preferences = getSharedPreferences(KEY,MODE_PRIVATE);
        preferencesEditor = preferences.edit();

        if(kat == "Geografie"){
            geografie.setChecked(true);
            geografie.setClickable(true);
            button_geografie = findViewById(R.id.button_geografie);
            button_geografie.setBackgroundColor(Color.parseColor("#FBBC55"));
            unlock_geografie = true;
            preferencesEditor.putBoolean("unlock_geografie", true);
}

Thanks!

maybe you have not transparent foregtround? try to use postInvalidate() on button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM