How to mute the toggle button for the entire app
I have a line of code to mute the sound for the whole application
<item name="android:soundEffectsEnabled">false</item>
And it works great, but I want to turn the sound on / off by clicking the toggle button.
So, can anyone please tell me what I can do for a runtime change theme for the whole application.
EDIT: UPDATED CODE
mToggleBtnSound.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean on = ((ToggleButton) v).isChecked();
if (on) {
// Change Whole App Theme
MyApplication.changeToTheme(getApplicationContext(), MyApplication.THEME_SOUND_ON);
//Save state of toggle button yes or no
MyApplication.getAppliation().getDeviceResourceHandler()
.addToSharedPref(Constant.SHARED_PREF_IS_SOUND_ON, true);
} else {
MyApplication.changeToTheme(getApplicationContext(), MyApplication.THEME_SOUND_OFF);
MyApplication.getAppliation().getDeviceResourceHandler()
.addToSharedPref(Constant.SHARED_PREF_IS_SOUND_ON, false);
}
}
});
Change theme in app class
package com.my.app;
import android.content.Context;
import com.my.app.MyApplication;
public class MyApplication extends Application {
private static int sTheme;
public final static int THEME_SOUND_ON = 0;
public final static int THEME_SOUND_OFF = 1;
@Override
public void onCreate() {
super.onCreate();
}
public static void changeToTheme(Context context, int theme) {
sTheme = theme;
switch (sTheme) {
default:
case THEME_SOUND_ON:
context.setTheme(R.style.AppSoundOnTheme);
break;
case THEME_SOUND_OFF:
context.setTheme(R.style.AppSoundOffTheme);
break;
}
}
}
source to share
If my understanding is correct, this should help you.
ToggleButton tbtn=(ToggleButton)findViewById(id);
tbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
// Sound is disabled
tbtn.setSoundEffectsEnabled(false);
} else {
// Sound is enabled
tbtn.setSoundEffectsEnabled(true);
}
}
});
source to share
A possible workaround for your requirement is to define two themes in your styles. xml, one with <item name="android:soundEffectsEnabled">false</item>
and one with <item name="android:soundEffectsEnabled">true</item>
. Then, in the setOnCheckedChangeListener method of the toggle button, set the appropriate application theme. Here you can change theme of theme Change app theme and also find many other examples for changing theme by searching on Google.
Hope it helps :)
source to share
You can solve the problem using the method below.
public static void changeToTheme(Context context, int theme) {
sTheme = theme;
switch (sTheme) {
default:
case THEME_SOUND_ON:
context.setTheme(R.style.AppSoundOnTheme);
break;
case THEME_SOUND_OFF:
context.setTheme(R.style.AppSoundOffTheme);
break;
}
recreate(); //put this line in your code
}
source to share