I have the line of code for turn off sound for whole application
<item name="android:soundEffectsEnabled">false</item>
And it works fine, but I want to turn sound on/off by clicking toggle button .
So anyone suggest me that what can I do for change theme at run time for 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 into Application 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;
}
}
}