I have this checkbox which is supposed to mute all the sounds when it is checked. When I run the app and the checkbox is unchecked the sound is on. Then I check the box and the sounds are muted! When I uncheck the checkbox the sounds remain muted. What am I doing wrong??
Here is the code I use
public class Settings extends Activity {
CheckBox chb;
boolean m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
final SharedPreferences prefs = this.getSharedPreferences("mute", Context.MODE_PRIVATE);
m = prefs.getBoolean("mute", false);
chb = (CheckBox) findViewById(R.id.checkBox1);
chb.setChecked(m);
final AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_MUSIC, m);
chb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( buttonView.isChecked() == true )
{
m = true;
amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("mute", true);
editor.commit();
}
if (buttonView.isChecked() == false)
{
m = false;
System.out.println("unmute");
amanager.setStreamMute(AudioManager.STREAM_MUSIC, false);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("mute", false);
editor.commit();
}
}
});
}
I update in case someone finds it usefull.
The mute requests for a given stream are cumulative: the AudioManager can receive several mute requests from one or more clients and the stream will be unmuted only when the same number of unmute requests are received.
For a better user experience, applications MUST unmute a muted stream in onPause() and mute is again in onResume() if appropriate.
In my case the problem was that when I check the checkbox I mute the sounds. When I go back to the settings activity this code is executed again
final SharedPreferences prefs = this.getSharedPreferences("mute", Context.MODE_PRIVATE);
m = prefs.getBoolean("mute", false);
chb = (CheckBox) findViewById(R.id.checkBox1);
chb.setChecked(m);
final AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_MUSIC, m);
and mutes the sounds again! So we have 2 mutes (cumulative) and we need to unmute it twice.
The problem is that the listener is called before the view is updated, so you need to use the isChecked passed to you
Switch your onchecked listener to this:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked == true )
{
m = true;
amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("mute", true);
editor.commit();
}
else
{
m = false;
System.out.println("unmute");
amanager.setStreamMute(AudioManager.STREAM_MUSIC, false);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("mute", false);
editor.commit();
}
}
Related
I have an issue where I am trying to change the foreground of a Button upon a click event. The foreground only changes after the first click.
public void muteSoundClick(View view){
playSound = preferences.getBoolean("playSound", true);
if (playSound) {
view.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
editor.putBoolean("playSound", false);
editor.apply();
}
else {
view.setForeground(getDrawable(R.drawable.foreground_padding_mute));
editor.putBoolean("playSound", true);
editor.apply();
}
}
I retrieve a variable from the android SharedPrefernces, which i use to determine which foreground to use.
First you need to check playsound at the start of the activity so that the button itself would get what you are saving the first time so you need something like this :
playSound = preferences.getBoolean("playSound", true);
if(playSound)
button.setForeground(getDrawable(R.drawable.foreground_padding_mute));
else
button.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
Then you can handle the clicks using the same method you have posted in your question
here is a sample example activity to sum it up :
public class SimpleActivity extends AppCompatActivity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
boolean playSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
editor= PreferenceManager.getDefaultSharedPreferences(this).edit();
Button button = findViewById(YourButtonID);
playSound = preferences.getBoolean("playSound", true);
if(playSound)
button.setForeground(getDrawable(R.drawable.foreground_padding_mute));
else
button.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
}
public void muteSoundClick(View view){
playSound = preferences.getBoolean("playSound", true);
if (playSound) {
view.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
editor.putBoolean("playSound", false);
editor.apply();
}
else {
view.setForeground(getDrawable(R.drawable.foreground_padding_mute));
editor.putBoolean("playSound", true);
editor.apply();
}
}
}
It's seems that you aren't really editing the preferences. Like this.
SharedPreferences.Editor editor;
public void muteSoundClick(View view){
playSound = preferences.getBoolean("playSound", true);
if (playSound) {
editor = sharedPreferences.edit();
view.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
editor.putBoolean("playSound", false);
editor.commit();
}
else {
view.setForeground(getDrawable(R.drawable.foreground_padding_mute));
editor.putBoolean("playSound", true);
editor.apply();
}
}
Hello i don't know how can i let my app save switch state
Example:
I want open the app and i want switch off the switch after restart my app i want my app keep the states off and vice versa
public class AlgeriaNotificationsActivity extends AppCompatActivity {
public static final String PREFS_NAME = "MyPrefsFile";
private TextView switchStatus;
private Switch mySwitch;
private static final String TAG = "AlgeriaNotificationsActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_algeria_notifications);
switchStatus = (TextView) findViewById(R.id.switchStatus);
mySwitch = (Switch) findViewById(R.id.mySwitch);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("switchkey", false);
mySwitch.setChecked(silent);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
switchStatus.setText("Switch is currently ON");
}else{
switchStatus.setText("Switch is currently OFF");
}
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("switchkey", isChecked);
}
});
//check the current state before we display the screen
if(mySwitch.isChecked()){
switchStatus.setText("Switch is currently ON");
}
else {
switchStatus.setText("Switch is currently OFF");
}
}
}
You can use SharedPreferences for same, when switch is check event occur just save its value to true else set false.
https://developer.android.com/guide/topics/data/data-storage.html#pref
in activity oncreate just check fro sharedprefernce value if it is true set switch on else set off
oncreate check for switch value
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("switchkey", false);
mySwitch.setChecked(silent);
code for save state in switch checkchange
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
switchStatus.setText("Switch is currently ON");
}else{
switchStatus.setText("Switch is currently OFF");
}
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("switchkey", isChecked);
editor.commit();
}
I am developing a game on Android, I have a background music that stop with a switch and that is in an other activity, that of the parameters. The music is in the main menu.
I would like to know how to save the state of the switch (checked or not checked) by going from the main menu to the parameters
This the code for my switch :
buttonmusique = (Switch) findViewById(R.id.switchMusique);
buttonmusique.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.e("bouton", String.valueOf(buttonmusique.isChecked()));
// If the music is playing
if (isChecked) {
Intent music = new Intent();
music.setClass(Parametres.this, BackgroundMusic.class);
startService(music);
musicBoolean = true;
buttonmusique.setText("Musique On"); //To change the text near to switch
Log.d("You are :", "Checked");
} else {
Intent music = new Intent();
music.setClass(Parametres.this, BackgroundMusic.class);
stopService(music);
musicBoolean = false;
// Resume the music player
buttonmusique.setText("Musique OFF"); //To change the text near to switch
Log.d("You are :", " Not Checked");
}
}
});
Put it in your create method: (+adapt to your situation)
final SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
CheckBox checkbox = (CheckBox) findViewById(R.id.myCheckBox);
checkbox.setChecked(sharedPreferences.getBoolean("mycheckbox", true));
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
sharedPreferences.edit().putBoolean("mycheckbox", b).apply();
}
});
You can save it in SharedPreferences.
SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
Save sound settings
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(getString(R.string.sound_enabled), isChecked);
editor.commit();
To get sound settings use:
boolean soundEnabled = prefs.getBoolean(getString(R.string.sound_enabled), false);
// Set switch state
buttonmusique.setChecked(soundEnabled);
Load status:
boolean switch_status = PreferenceManager.getDefaultSharedPreferences(ctx).getBoolean("switch_status", false);
Save status:
SharedPreferences.Editor sped = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
sped.putBoolean("switch_status", value);
sped.commit();
Use shared preferences or a database to store the state of your switch. It is essential that you depend on the lifecycle methods of Activity/fragment.
The following might help you:
......
.....
#Override
public void onClick(View v)
{
if (toggle.isChecked())
{
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("state_to_save", true);
editor.commit();
}
else
{
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("state_to_save", false);
editor.commit();
}
}
The final Code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
.......
......
SharedPreferences sharedPrefs = getSharedPreferences("com.example.xyle", MODE_PRIVATE);
toggle.setChecked(sharedPrefs.getBoolean("state_to_save", true));
}
Hey guys I want to make my checkbox stay in the same state every time I open my app.. I get this with the 'ja/nein' string, the string states when i close and open again my application... but my checkbox.setchecked(true/false) doesnt work.. please help
public void changeVisitStatus(){
SharedPreferences visitStatus = mData.getVisitStatus();
SharedPreferences.Editor editor = visitStatus.edit();
if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
editor.putString(mData.mVisitKey, "ja");
editor.commit();
mGUI.mBtnVisit.setChecked(true);
}
else{
editor.putString(mData.mVisitKey, "nein");
editor.commit();
mGUI.mBtnVisit.setChecked(false);
}
mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}
EDIT: I tried it another way.. I thought it would be better but doesnt work as well..
public void changeVisitStatus(){
SharedPreferences visitStatus = mData.getVisitStatus();
SharedPreferences.Editor editor = visitStatus.edit();
if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
editor.putString(mData.mVisitKey, "ja");
editor.putBoolean("isChecked", true);
editor.commit();
}
else{
editor.putString(mData.mVisitKey, "nein");
editor.putBoolean("isChecked", false);
editor.commit();
}
mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}
and put this one into my onCreate(Bundle savedInstanceState) in my Activity
mGUI.mBtnVisit.setChecked(mData.getVisitStatus().getBoolean("isChecked", false));
Try like this:
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
}
and in onCreate()
CheckBox checkBox = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkBox = (CheckBox) findViewById(R.id.my_check_box);
boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
checkBox.setChecked(isChecked);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.i("boolean",""+isChecked);
TestActivity.this.putBooleanInPreferences(isChecked,"isChecked");
}
});
}
Hope this may help you!
You're showing us only the code for changing the status, probably called from OnClick listener for the check box.
You should also add code that only reads status from SharedPreferences and sets check box state according to that (could be same code, but the if condition negated).
You need to call that code from OnCreate event.
public void setVisitStatus(){
SharedPreferences visitStatus = mData.getVisitStatus();
mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}
i want to make 4 different button as check and un check even after the app get closed,i am using shared preferences for that but i cant able to maintain the state,its confusing me.can anyone help me.
Thank you
preferences1 = PreferenceManager
.getDefaultSharedPreferences(Notification_Dashboard.this);
Boolean state_chk = preferences1.getBoolean("key", false);
System.out.println("state_chk" + state_chk);
if (state_chk == true) {
msg_viewed = "0";
message_view.setBackgroundResource(R.drawable.notif_uncheck);
preferences1.edit().putBoolean("key", false).commit();
}
else {
msg_viewed = "1";
message_view.setBackgroundResource(R.drawable.notif_checked);
preferences1.edit().putBoolean("key", true).commit();
}
You could try something like this :
For your checkbox define the checkedchanged listner as below:
yourcheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
saveCheckBox("First",true);
} else {
saveCheckBox("First",false);
}
}
});
Save checkbox value in sharedpreference
public void saveCheckBox(String key, boolean isChecked){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(key,isChecked);
editor.commit();
}
Get the preference values
public boolean getCheckBoxState(String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.getBoolean(key, false);
}
In your activity onCreate() method try to get sharedpreference value and set the checkbox checked or uncheck as below:
Boolean isCheck=getCheckBoxState("First");
if(isCheck)
{
yourcheckbox.setChecked(true);
}
else
{
yourcheckbox.setChecked(false);
}
I hope this will help you.
Thanks.