Simple settings menu with Checkboxpreference Android

Asked

Viewed 418 times

1

I have a simple settings menu where I have a CheckBoxPreference. I want to set silent mode when the checkbox is checked and normal mode when it is not checked. The sound code I already have, but I’m having trouble getting the value of checkBoxPreference,since I only have the settings class that extends PreferenceActivity. Follow the code below:

Prefs.java

package br.com.pedro.menu;

import br.com.pedro.school.R;

import android.content.Context;

import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Prefs extends PreferenceActivity {
    AudioManager som;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);

    // USE AudioManager for Settingringing from vibration
    som = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

}

}

xml/prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory android:title="@string/geral" >

    <CheckBoxPreference
        android:key="silenciar"
        android:summary="@string/mudo_inst"
        android:title="@string/mudo" />
</PreferenceCategory>

please help me!

1 answer

1

Change Prefs.java to register a OnSharedPreferenceChangeListener.

package br.com.pedro.menu;

import br.com.pedro.school.R;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Prefs extends PreferenceActivity {
    AudioManager som;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);    

} 

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {

    //Teste pelo valor que foi alterado e actue em conformidade
    if(preference.getKey().equals("silenciar"){

        if((boolean) newValue){
            //ligar som
        }
        else
        {
            //desligar som
        }
    }
    return true;
}  

If you just want to test the CheckBox should register a OnPreferenceChangeListener

Preference ckboxPref = this.findPreference("silenciar");
ckboxPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        //Testar o novo valor
        if((boolean) newValue){
            //ligar som
        }
        else
        {
            //desligar som
        }
        return true;
    }
});

Following your comment I will try to explain what is happening.

Whenever you change the value of CheckBox the method onPreferenceChange is called. There, according to the value of the CheckBox, you turn the sound on or off. How the method returns true the value is saved.

I think or your problem is when your application starts.
You will have to do the same test on MainActivity of its application.

In the onCreate you have to test the value of CheckBox.

@Override
protected void onCreate(Bundle savedInstanceState) {
    .......
    .......
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

    boolean silenciar = preferences.getBoolean("silenciar", false);
    if(silenciar){
        //desligar som
    }
    else
    {
        //ligar som
    }
}
  • How could I Test the checkbox and save it at the same time?

  • What do you mean, save him?

  • Use of class SharedPreferences for when the user terminates the application and returns, Checkboxpreference is marked in the way that it set previously!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.