Aviroxi
Aviroxi

Reputation: 115

.setText(" text ") is not working properly in android studio

I am new to android development I want to create an app that transforms light mode to dark mode I have a button called enable dark mode I want to enable the dark mode when I press that button, I turned the dark mode ON but I also want to change the text in the button to enable light mode after clicking the button. when I use the setText() method to change the text in the onclick method the text in the box changes only when I press the button 2 times but I want the text to change instantly after pressing the button for the first time Here is my java code

public class MainActivity extends AppCompatActivity {

    private Button darkmode;

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

        darkmode = (Button) findViewById(R.id.darkmodebutton);

        darkmode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                darkmode.setText("enable light mode");
            }
        });
    }
}

can anyone help me, please here is how the app is behaving I made a gif

app behaviour

Upvotes: 1

Views: 788

Answers (2)

Lalit Fauzdar
Lalit Fauzdar

Reputation: 6363

My Take on this:

You've not shown changing it to Light mode which I'm sure will reflect the same behaviour.

What happens is when you change the default mode using setDefaultNightMode(), it re-creates the activity the very moment this line gets executed which means your code never reaches the second line setText() when it changes the default mode as written in the image attached below. You can read further here in official docs.

enter image description here

But, when the mode has already been changed, the first line with setDefaultNightMode() does nothing as the mode has already been changed which leads to changing the text second time, which means to change the text, either there should be a callback mechanism or you've to change the text prior to changing the mode.

Further, you can get the current mode when activity restarts and is created and set the text according to the current mode which should definitely be the go to way.

So, to check the mode on Actvity creation or recreation, you can do as:

int currentNightMode = getResources().getConfiguration().uiMode
        & Configuration.UI_MODE_NIGHT_MASK
when(currentNightMode) {
    Configuration.UI_MODE_NIGHT_NO ->
        // Night mode is not active, we're in day time
    Configuration.UI_MODE_NIGHT_YES ->
        // Night mode is active, we're at night!
    Configuration.UI_MODE_NIGHT_UNDEFINED ->
        // We don't know what mode we're in, assume notnight
}

And set the button's text based on the aforementioned cases.

Your answer made me read about this which I also wanted to implement and I found a good article for this from which the code has been referenced, read here - DayNight — Adding a dark theme to your app

Upvotes: 1

Atick Faisal
Atick Faisal

Reputation: 1824

I think the app lifecycle resets when the theme is changed. You can use a lifecycle aware state variable to track the theme state.

You can read more about it here.

Boolean isLightThemeEnabled = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        theme = savedInstanceState.getString("THEME_KEY");
    }

    // YOUR USUAL CODE
    // MAKE SURE TO UPDATE THE THEME STATE VARIABLE
    
}


@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    if(!isLightThemeEnabled) {
         darkmode.setText("enable light mode");
    }
    else {
        .....
    }
}


@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putString("THEME_KEY", isLightThemeEnabled);
    
    super.onSaveInstanceState(outState);
}

Upvotes: 1

Related Questions