Change text of button immediately

I am currently trying to apply a 'toggle' kind of system that switches the button's text depending upon the boolean it represents.

For example, when boolean RequestingLU true, I want the button to say 'stop', and if false, say 'start'.

Currently I've put it in onStart(); so that it would at least update whenever I visit the screen,

public void onStart() {
    super.onStart();
    final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    final SharedPreferences.Editor editor = settings.edit();
    final Button StopButton = (Button) findViewById(R.id.StopButton);
    boolean RequestingLU = settings.getBoolean("RequestingLU", true);
    if (RequestingLU) {
        StopButton.setText(R.string.Stop_Button);
        StopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainMenu.this);
                StopDialog.setTitle(R.string.Stop_Title);
                StopDialog.setMessage(R.string.Stop_Message);
                StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        editor.putBoolean("RequestingLU", false);
                        editor.apply();
                        Toast.makeText(MainMenu.this, "You have stopped the app", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Closes box
                        dialog.dismiss();
                    }
                });
                StopDialog.create().show();
            }
        });
    }
    else {
        StopButton.setText(R.string.Start_Button);
        StopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editor.putBoolean("RequestingLU", true);
                editor.apply();
                Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

I want the button functioning so that the change applies immediately after the user presses 'dismiss'. How can I achieve this?

Upvotes: 2

Views: 57

Answers (2)

Vijayaramanan
Vijayaramanan

Reputation: 80

I hope it will work fine for you public void onStart() { super.onStart(); final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); final SharedPreferences.Editor editor = settings.edit(); final Button StopButton = (Button) findViewById(R.id.StopButton); boolean RequestingLU = settings.getBoolean("RequestingLU", true);

    StopButton.setText(RequestingLU?R.string.Stop_Button:R.string.Start_Button);
    StopButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
           final boolean RequestingLUSwitch = settings.getBoolean("RequestingLU", true);
            if(RequestingLUSwitch) {
                AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainMenu.this);
                StopDialog.setTitle(R.string.Stop_Title);
                StopDialog.setMessage(R.string.Stop_Message);
                StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        updatePreference(!RequestingLUSwitch);
                        Toast.makeText(MainMenu.this, "You have stopped the app", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Closes box
                        dialog.dismiss();
                    }
                });
                StopDialog.create().show();
            }else{
                updatePreference(!RequestingLUSwitch);
                Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show();
            }
        }
    });


}

private void updatePreference(boolean requestingSwitch){
 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
 SharedPreferences.Editor editor = settings.edit();
 editor.putBoolean("RequestingLU", !RequestingLUSwitch);
 editor.apply();
StopButton.setText(!RequestingLUSwitch ? R.string.Stop_Button : R.string.Start_Button);

}

Upvotes: 1

TWL
TWL

Reputation: 6646

put your StopButton.setText() inside your onClicks

Upvotes: 0

Related Questions