Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get android preferences to work?
    text
    copied!<p>Sorry about this guys. This must be like the third question I've had to ask about this project alone. =(</p> <p>What i want to be able to do is toggle the visibility of a radio button using a check box in a preferences activity.</p> <p>There are no errors in my code and both activities run fine, but there is no change in visibility when the check box is checked or unchecked.</p> <p>Here is my Activity with the radiobuttons</p> <pre><code>import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; import android.widget.EditText; import android.widget.RadioButton; import com.medialets.android.analytics.MMAnalyticsManager; public class Temperature extends Activity { MMAnalyticsManager mManager; private EditText text; public void onStart(Bundle savedInstanceState){ super.onStart(); getPrefs(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.temperature); text = (EditText)findViewById(R.id.EditText01); mManager = MMAnalyticsManager.sharedInstance(this); mManager.start("codeHere", "1.0", false); } @Override public void onPause() { super.onPause(); mManager.pause(); } @Override public void onResume() { super.onResume(); mManager.resume(); } @Override public void onDestroy() { super.onDestroy(); mManager.stop(); } public void myClickHandler(View view){ switch (view.getId()) { case R.id.Button01: RadioButton celsiustokelvinButton = (RadioButton) findViewById (R.id.RadioButton05); RadioButton celsiustofarenheitButton = (RadioButton) findViewById(R.id.RadioButton01); RadioButton farenheittocelsiusButton = (RadioButton) findViewById(R.id.RadioButton02); RadioButton farenheittokelvinButton = (RadioButton) findViewById(R.id.RadioButton06); RadioButton kelvintocelsiusButton = (RadioButton) findViewById (R.id.RadioButton03); RadioButton kelvintofarenheitButton=(RadioButton) findViewById (R.id.RadioButton04); float inputValue = Float.parseFloat(text.getText().toString()); if (celsiustofarenheitButton.isChecked()) { text.setText(String .valueOf(convertCelsiusToFarenheit(inputValue))); } if (farenheittocelsiusButton.isChecked()) { text.setText(String .valueOf(convertFarenheitToCelsius(inputValue))); } if (celsiustokelvinButton.isChecked()) { text.setText(String .valueOf(convertCelsiusToKelvin(inputValue))); } if (farenheittokelvinButton.isChecked()) { text.setText(String .valueOf(convertFarenheitToKelvin(inputValue))); } if (kelvintocelsiusButton.isChecked()) { text.setText(String .valueOf(convertKelvinToCelsius(inputValue))); } if (kelvintofarenheitButton.isChecked()) { text.setText(String .valueOf(convertKelvinToFarenheit(inputValue))); } break; }} private float convertFarenheitToCelsius(float fahrenheit){ return ((fahrenheit - 32) * 5 / 9); } private float convertCelsiusToFarenheit(float celsius){ return ((celsius * 9) / 5) + 32; } private double convertCelsiusToKelvin(float celsius){ return celsius + 273.15; } private double convertFarenheitToKelvin(float farenheit){ return ((farenheit-32)/(1.8)); } private double convertKelvinToCelsius(float kelvin){ return kelvin-273.1; } private double convertKelvinToFarenheit(float kelvin){ return kelvin*1.8-459.67; } boolean CheckboxPreference; private void getPrefs() { // Get the xml/preferences.xml preferences SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(getBaseContext()); CheckboxPreference = prefs.getBoolean("checkboxPref", true); } } </code></pre> <p>And here is my Preferences Activity </p> <pre><code>import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.Preference.OnPreferenceClickListener; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.Toast; import android.widget.CompoundButton.OnCheckedChangeListener; public class Preferences extends PreferenceActivity { private RadioButton btn01; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); btn01 = (RadioButton)findViewById(R.id.RadioButton01); Preference customPref = (Preference) findPreference("customPref"); customPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){ public boolean onPreferenceClick(Preference preference) { Toast.makeText(getBaseContext(),"The Custom Preference Has Been Clicked",Toast.LENGTH_LONG).show(); SharedPreferences customSharedPreference = getSharedPreferences("myCutomSharedPrefs", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = customSharedPreference.edit(); editor.putString("myCustomPref","The preference has been clicked"); editor.commit(); CheckBox(); return true; } private void CheckBox() { final CheckBox ThisCheckBox = (CheckBox) findViewById (R.id.checkboxPref); ThisCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton compoundButton,boolean test) { if (ThisCheckBox.isChecked()){ btn01.setVisibility(View.VISIBLE); } else { btn01.setVisibility(View.GONE); } } }); }; }); }} </code></pre> <p>Sorry there is other stuff in there as well such as an analytics plug in</p> <p>Anyone have any idea as to why this just isn't working</p> <p>(Just a side note. When looking at the logcat, clicking the checkbox returned no results)</p> <p><strong>EDIT:</strong> OK i've just ammended my code as suggested but i'm afraid that the preferences still do not affect anything. Anybody have any ideas?? =(</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload