setContentView of the application - android

So I have checkbox in settings to enter the different xml
<CheckBoxPreference android:title="Full Screen"
android:defaultValue="false"
android:key="checkbox3"
I use if checkbox3 is not checked then setContentView(1st xml) if it is checked setContentView(second xml)
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean samples = getPrefs.getBoolean("checkbox3", true);
if(samples==false)
setContentView(R.layout.activity_main);
else{
setContentView(R.layout.fullscreen);
}
My problem is that default value of checkbox is false so when you first run application it should setContentView(R.layout.activity_main); but it enters setContentView(R.layout.activity_main); even if it is "false" in settings.

Change boolean samples = getPrefs.getBoolean("checkbox3", true); to boolean samples = getPrefs.getBoolean("checkbox3", false);
You have set the default value to true instead of false

You are mistaken the default value of Check box is true according to your code:
boolean samples = getPrefs.getBoolean("checkbox3", true);
Change it to
boolean samples = getPrefs.getBoolean("checkbox3", false);
And you be fine

Related

Muting Sounds in Android App with Shared Preferences Not Working

I'm building my first Android app, so apologies in advance for my lack of expertise. I've been banging away at this issue for 2 days so far. When I set the sound to mute in my Settings Activity via Shared Preferences, the setting does not 'stick'. Here is the code from my Main Activity:
SharedPreferences settingsSP;
boolean muteSound;
Then in onCreate:
settingsSP = getApplicationContext().getSharedPreferences("PlayerPreferences", Context.MODE_PRIVATE);
muteSound = settingsSP.getBoolean("muteSound", false);
Then I obviously use the muteSound variable to determine whether or not to play sounds within the MainActivity. Now when I go the Settings activity I have this code:
SharedPreferences settingsSP;
boolean muteSound;
Then in onCreate I have this:
settingsSP = getApplicationContext().getSharedPreferences("PlayerPreferences", Context.MODE_PRIVATE);
if (muteSound) {
activitySettingsBinding.displayMuteCheckBox.setChecked(true);
} else {
activitySettingsBinding.displayMuteCheckBox.setChecked(false);
}
activitySettingsBinding.displayMuteCheckBox.setOnClickListener(v -> {
if (activitySettingsBinding.displayMuteCheckBox.isChecked()) {
muteSound = true;
SharedPreferences.Editor editor = settingsSP.edit();
editor.putBoolean("muteSound", muteSound);
editor.apply();
} else {
muteSound = false;
SharedPreferences.Editor editor = settingsSP.edit();
editor.putBoolean("muteSound", muteSound);
editor.apply();
soundPool.play(bubble, 1, 1, 1, 0, 1);
}
The xml code:
<CheckBox
android:id="#+id/displayMuteCheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:gravity="center_vertical"
android:layoutDirection="rtl"
android:paddingLeft="40dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="Mute Sound"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
android:theme="#style/CheckBoxStyle" />
I placed a number of log.d tags throughout the code so that I observe what was happening. For example after I cold booted the app in Android Studio:
Open MainActivity, onCreate muteSound = false
Tap Settings button and the Settings onCreate lists muteSound = false
I check the checkbox to set it to muteSounds = true
I tap return button within Settings to return to MainActivity
MainActivity onCreate muteSound = true
Tap Settings button and the Settings onCreate lists muteSound = false
And the checkbox is unchecked - because muteSound = false
I can't figure out why the settings is not sticking. When I checked to set the muteSound to be true, that settings is lost when I return to the Settings Activity. I've searched the code to see if I set muteSound = true on accident somewhere else, but when searching the usages I did not find any. Is my assumption about sharePreferences incorrect. It doesn't appear to hold the settings. Or perhaps am I using the checkbox incorrectly, maybe I should be using another type of button here. I would like to use checkboxes to control the display of the score, time and other items on the MainActivity and throughout other app Activities, but if I can't figure this gap in my knowledge I won't be able to. What am I doing incorrectly?
For displayMuteCheckBox, use setOnCheckedChangeListener instead of setOnClickListener and use settingsSP variable to store value in SharedPreferences instead of creating new object of SharedPreferences.Editor:
activitySettingsBinding.displayMuteCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
settingsSP.edit().putBoolean("muteSound", isChecked).apply();
if (isChecked)
soundPool.play(bubble, 1, 1, 1, 0, 1);
}
});

boolean SharedPreferences always loading default values

I've a strange issue with SharedPreferences and boolean.
I've set this code in my xml:
xml:
<CheckBoxPreference
android:key="onlywifiupload"
android:defaultValue="true"
android:summary="#string/summary_onlywifiupload"
android:title="#string/title_onlywifiupload"
/>
and from the Java code, I'm calling:
boolean onlywifiupload = pref.getBoolean("onlywifiupload", true);
Even the checkbox is checked or unchecked, in onlywifiupload there's always true.
Same with setting:
boolean onlywifiupload = pref.getBoolean("onlywifiupload", false);
It seems the default value is always loaded instead of checked values.
It seems the only way to make it working is:
mPrefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
onlywifiupload = mPrefs.getBoolean("onlywifiupload", true);
don't know why I need to call getDefaultSharedPreferences from PrefenceManager object
Before that, I was calling the preferences in this way:
pref = getSharedPreferences("AppPref", MODE_PRIVATE);

Is there only one shared preferences object per application?

My question is: If I call:
PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
in two different Activities in my application, does it refers to the same SharedPreferences file?
More concretely, I have a rememberMe check box in my SharedPreferences.
which I can change from two locations in my application. It looks like when I change it in one location it doesn't take effect in the second location.
Edit:
I have this code:
public View createSettingsOverlay(){
ViewGroup root = (ViewGroup)findViewById(R.id.absoluteOverlay);
LayoutInflater inflater = getLayoutInflater();
View result = inflater.inflate(R.layout.overlay_baloon_settings, root, false);
((TextView)result.findViewById(R.id.loginText)).setText(application.getCurrentlyLoggedUser());
((TextView)result.findViewById(R.id.passwordText)).setText(application.getCurrenlyLoggedPass());
((TextView)result.findViewById(R.id.loginTimeText)).setText(application.getTimeOfLogin().toString());
((TextView)result.findViewById(R.id.settings_popup_server_url)).setText(application.getCurrentUrl());
//Emil Edit
CheckBox rememberMe = (CheckBox)result.findViewById(R.id.cbRememberMe);
//boolean rememberMePreference = PreferenceManager.getDefaultSharedPreferences(DynamicDataActivity.this).getBoolean(SettingsActivity.REMEMBER_ME_CHECKBOX_KEY, false);
Log.d(TAG, "Remember Me set in the preference is: " + PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(SettingsActivity.REMEMBER_ME_CHECKBOX_KEY, false));
rememberMe.setChecked(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(SettingsActivity.REMEMBER_ME_CHECKBOX_KEY, false));
rememberMe.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "boolean isChecked is: " + isChecked);
if (isChecked)
{
Log.d(TAG, "Remember me checkbox in setting overlay set to True");
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean(SettingsActivity.REMEMBER_ME_CHECKBOX_KEY, true);
application.setRememberMeChecked(true);
}
else
{
Log.d(TAG, "Remember me checkbox in setting overlay set to False");
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean(SettingsActivity.REMEMBER_ME_CHECKBOX_KEY, false);
application.setRememberMeChecked(false);
}
}
});
Which basically created a popup window with setting from SharedPreferences when I check the rememberMe checkbox in this popup window, close it and open it again. for some reason it is not checked again, in other Activity (Setting Activity), I can see that this change didn't took effect as well.
Uses this for get SharedPreferences (myPrefs is your file):
For save:
SharedPreferences prefs = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("rememberMe", yourState);
editor.commit();
For get:
SharedPreferences prefs = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
boolean state = prefs.getBoolean("rememberMe", state_by_default);
Converting comments to answer,
Your problem is you haven't commit your preference. SharedPreference will update only after commit
Yes, default shared pref file is same for Application context.

Android: CheckBoxPreference enable/disable email address inputtype (override default settings)

Hey everyone, I'm trying to make an activity so that when a CheckBox Preference is enabled (true), programmatically the on-screen keyboard InputType will change. I want the user to have the option to turn on/off the keyboard's email address option while keeping the inputType's already set.
Essentially doing the equivalent of changing:
android:inputType="textAutoCorrect|textCapSentences|textMultiLine"
to
android:inputType="typeAutoCorrect|textCapSentences|textmultiLine|textEmailAddress"
Preferences.xml
<CheckBoxPreference android:key="pref_key_enable_email"
android:title="#string/pref_title_enable_email"
android:summary="#string/pref_summary_enable_email"
android:defaultValue="false" />
This is what I have as of now.
public void setInputType(int type) {
boolean showEmail = false;
// Show the Email keyboard if the pref_key_enable_email preference is TRUE
mTextEditor = (EditText) findViewById(R.id.embedded_text_editor);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean("pref_key_enable_email",false)== true){
showEmail = true;
if (showEmail) {
mTextEditor.setInputType(InputType.TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_EMAIL_ADDRESS); w
} else {
mTextEditor.getInputType();
}
}
I've checked the /data/data/myappname/shared_prefs com.myappnamehere.preferences.xml. I at least know that the boolean values do change from false to true when the box is checked. It just doesn't do anything :(
Consider cleaning your code a little bit
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean showEmail = prefs.getBoolean("pref_key_enable_email",false);
mTextEditor = (EditText) findViewById(R.id.embedded_text_editor);
if(showEmail) {
mTextEditor.setInputType( mTextEditor.getInputType() | TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
}
I'm assuming you have your "default" flags in the xml for your layout:
android:inputType="textAutoCorrect|textCapSentences|textMultiLine"

How to enable a preference in my android application when other preference is disabled?

I have used PreferenceActivity to have preference in my android application. I want one preference say "pref 2" to be enabled when other preference say "pref 1" is NOT checked and "pref 2" to be disabled when "pref 1" is checked.
i.e. exactly opposite of the android:dependancy attribute.
How can I do that?
Yes it's possible to do this out of the box.
Let's say you want to disable pref2 when pref1 is off
Here's the code(preference xml layout) to put in for pref1:
<CheckBoxPreference
android:title="Pref1"
android:key="pref1">
</CheckBoxPreference>
Here's the code(preference xml layout) to put in for pref2:
<EditTextPreference
android:dialogMessage="Pref 2 dialog"
android:title="Pref2"
android:key="pref2"
android:dependency="pref1">
</EditTextPreference>
Like sigmazero13 said, by default disableDependentsState is false so you don't need to include it in the pref1 attributes.
According to the docs here, you can add an attribute to the CheckBoxPreference tag like so:
android:disableDependentsState="true"
By default this is false, which means that the dependents are disabled when the checkbox is unchecked, but by setting it to true, it should have the opposite effect.
I don't think there's any out-of-the-box solution for it, i.e. an inverted dependancy attribute. But there's always the click-listener:
preference1.setOnPreferenceClickListener(pref1_click);
....
private OnPreferenceClickListener pref1_click = new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
preference2.setEnabled(!preference1.isChecked());
return true;
}
}
Android CheckBox??
I am assuming you are using the Android.widget.checkBox:
http://developer.android.com/reference/android/widget/CheckBox.html
Try this
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox1.isChecked()) {
checkBox2.setChecked(false);
}
}
}
GoodLUCK!!

Categories

Resources