4

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;
        }
    }
}
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
Garima Mathur
  • 3,312
  • 3
  • 18
  • 32
  • Why not use sharedpreferences to set a value to yes when the user changes the sound button and then where ever you use sound just check whether the sound value is yes?. This is persistent and should work fine?. might just take some coding. – Smashing May 26 '15 at 12:36
  • I have already done this, please check updated question. – Garima Mathur May 26 '15 at 12:49
  • @Garima - Call recreate() after setTheme() – Pankaj Jun 02 '15 at 11:55

3 Answers3

1

If my understanding is correct this should help you out.

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);
        }
    }
});
Randroid
  • 3,688
  • 5
  • 30
  • 55
  • The user wants to disable sound effects for whole application based on state of toggle button and not for the toggle button as you have answered here. Please read question properly before answering. – Alok Nair May 26 '15 at 12:10
  • @Alok Exactly but how can we achieve this – Garima Mathur May 26 '15 at 12:33
1

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 setOnCheckedChangeListener method of toggle button, set your application theme accordingly. You can get implementation of changing theme here Change application theme or can also find numerous other examples to change theme programatically by searching on Google.

Hope this helps :)

Community
  • 1
  • 1
Alok Nair
  • 3,994
  • 3
  • 24
  • 30
  • I tried this solution but its not working. setTheme() method does not change theme at runtime. – Garima Mathur May 26 '15 at 12:25
  • You have to recreate the current activity to apply the changes to theme. Also for other activities in back stack you have to recreate the activities in stack completely, only then the changes will be reflected in them. – Alok Nair May 27 '15 at 03:23
  • Also if you are using this technique, then in every activity check for your theme preference state set using toggle button and initialise the corresponding theme there before calling setContentView. – Alok Nair May 27 '15 at 03:27
0

Might solve your problem change in below method

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
    }
Pankaj
  • 7,908
  • 6
  • 42
  • 65