Question

I am currently looking to create sharedpreferences for a togglebutton. On selected, I want to set the mediaplayer setDataSource to audio1; on deselected, I want to set the mediaplayer setDataSource to audio2. I then want to implement the mediaplayer in the second activity, to play the correct audio depending on the toggle button..

I have come across a couple of problems browsing through the many tutorials out there, including: togglebutton resetting its state once exiting the activity; and the togglebutton not implementing the correct audio file.

Could anyone kindly point me in the right direction please?

public static final String PREFS_NAME = "MyPreferences";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_confidence_personalisation);

    final ToggleButton toggle = (ToggleButton) findViewById(R.id.togglePersonalise);

    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // What do I need to write here?
            } else {
                // What do I need to write here?
            }
        }
    });

Preference activity:

public class Preferences extends Activity {

public static final String PREFS_NAME = "MyPreferences";

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_confidence_personalisation);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);   

    final ToggleButton toggle = (ToggleButton) findViewById(R.id.togglePersonalise);

    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // Code to set MediaPlayer setDataSource to audio1
            } else {
                // Code to set MediaPlayer setDataSource to audio2
            }
        }
    });

Second activity playing corresponding audio:

public class Confidence extends ActionBarActivity {

public static final String PREFS_NAME = "MyPreferences";

        @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_confidence);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);       

    ImageButton b1 = (ImageButton) findViewById(R.id.button111);

    b1.setOnClickListener(new OnClickListener() {
        // Play audio corresponding to toggle button 
        @Override
        public void onClick(View v) {
            MediaPlayer mp1 = MediaPlayer.create(Confidence.this, R.raw.page12);
            mp1.start();
        }
    });
Was it helpful?

Solution

If you want to back it by a shared preference, you need to set the check state in onCreate to the value of the shared preference. Then in onCheckChanged you just set the value of the shared preference to the value isChecked

EDIT:

So in your new code- in the onClick of the play button use:

        MediaPlayer mp1 = MediaPlayer.create(Confidence.this, R.raw.page12);
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);       
        if(settings.getBoolean("ToggleName",true)){
            mp1.setDataSource(source1);
        }
        else{
            mp1.setDataSource(source2);
        }
        mp1.prepare();
        mp1.start();

In the two onCheckChanged functions, use

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);       
        SharedPreferences.Editor edit = settings.edit();
        edit.putBoolean("ToggleName",isChecked);
        edit.commit();

You may also want to look into using a PreferencesActivity, if you're making a standard Preference screen it will do a lot of the work for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top