I am trying to create a settings menu using preferenceFragments and preferenceActivities. When I select the Settings button I get an error that the app has crashed. I think I may be missing something. Are there any glaring errors? Thanks!!
I am calling it in the main activity here:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent i = new Intent(this, UserSettings.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
This is the UserSettings class:
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class UserSettings extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefsFragment()).commit();
}
public static class PrefsFragment extends PreferenceFragment{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.user_settings);
}
}
}
This is the user_settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Service Settings">
<CheckBoxPreference
android:defaultValue="true"
android:key="prefService"
android:summary="Enable Service"
android:title="Service" >
</CheckBoxPreference>
</PreferenceCategory>
<PreferenceCategory android:title="Other Settings" >
<CheckBoxPreference
android:defaultValue="false"
android:key="prefLockScreen"
android:summary="Enable Notifications"
android:title="Notify" >
</CheckBoxPreference>
<ListPreference
android:key="prefUpdateFrequency"
android:title="GPS settings"
android:summary="Set GPS Power Usage"
android:entries="#array/updateGPS"
android:entryValues="#array/updateGPSValues"
/>
</PreferenceCategory>
</PreferenceScreen>
I also have an array for the values from updateGPSValues in resources.
Related
How do I open up into a nested PreferenceScreen to a particular preference from an activity (such as through an intent)?
Example:
<PreferenceScreen
...
<!-- opens a subscreen of settings -->
<PreferenceScreen
android:key="sub_menu_key"
android:persistent="false"
android:title="Submenu">
...
<PreferenceCategory
android:key="category_key"
android:title="Category">
...
<Preference
android:key="tos_key"
android:title="Terms of Service" />
...
</PreferenceCategory>
</PreferenceScreen>
...
Is there a way to open directly to where "Terms of Service" is visible.
You just need to delare the specified Preference class in your onResume() method. In my case I was using SwitchPreference class, therefore the code would be like - SettingsActivity.class
public static class PrivacyPreferenceFragment extends PreferenceFragment {
public SwitchPreference switchPreference;
#Override
public void onResume() {
super.onResume();
switchPreference = (SwitchPreference) findPreference("privacy_notice_check");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_privacy);
setHasOptionsMenu(true);
}
}
Then in the activity where you want to use the PreferenceFragment value, just use the SharedPreference object to call the values and trigger it.
If you want the SharedPreference logic just comment below.
From an Activity (es: Main Activity) you can use an explicit intent
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button goToPreference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
goToPreference = (Button) findViewById(R.id.goToButton);
goToPreference.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// when you click go to Preference
Intent intent = new Intent( MainActivity.this, UserSettingsActivity.class);
startActivity(intent);
}
});
}
}
Layout: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="it.uniba.di.ivu.di.sms16.gruppox.examplesettings.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go To Preference"
android:id="#+id/goToButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
UserSettingsActivity is an activity that extends PreferenceActivity and allows you to view preference
import android.content.SharedPreferences;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.os.Bundle;
public class UserSettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals("lenguage_preference"))
{
// TO DO
}
}
#Override
protected void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
protected void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
}
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Lenguage">
<ListPreference
android:key="lenguage_preference"
android:title="Change Lenguage"
android:summary="Choose yout language"
android:entries="#array/entries_list_preference2"
android:entryValues="#array/entryvalues_list_preference2"
android:dialogTitle="choose favourite language" />
</PreferenceCategory>
<PreferenceCategory
android:title="Distance">
<ListPreference
android:key="distance_preference"
android:title="Distance"
android:summary="Change distance"
android:entries="#array/entries_list_preference"
android:entryValues="#array/entryvalues_list_preference"
android:dialogTitle="choose favourite distance" />
</PreferenceCategory>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="entries_list_preference"
translatable="false">
<item>2 (km)</item>
<item>1 (km)</item>
<item>500 (m)</item>
<item>200 (m)</item>
</string-array>
<string-array name="entryvalues_list_preference"
translatable="false">
<item>2000</item>
<item>1000</item>
<item>500</item>
<item>200</item>
</string-array>
<string-array name="entries_list_preference2"
translatable="false">
<item>Italian</item>
<item>English</item>
<item>French</item>
</string-array>
<string-array name="entryvalues_list_preference2"
translatable="false">
<item>it</item>
<item>en</item>
<item>fr</item>
</string-array>
Is there a way to open directly to "Terms of Service"?
I think you can refer to How to open or simulate a click on an android Preference, created with XML, programmatically?
I have followed the guide "settings" from android developers. But when I launch my SettingsActivity all I get is white screen. What am I doing wrong?
Here is my SettingActivity.java
public class SettingsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
Here is SettingFragment
public class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Here is preferences file
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference android:title="Your Name"
android:key="username"
android:summary="Please provide your username"></EditTextPreference>
<CheckBoxPreference android:title="Application Updates"
android:defaultValue="false"
android:summary="This option if selected will allow the application to check for latest versions."
android:key="applicationUpdates" />
<ListPreference android:title="Download Details"
android:summary="Select the kind of data that you would like to download"
android:key="downloadType"
android:defaultValue="1"
android:entries="#array/listArray"
android:entryValues="#array/listValues" />
</PreferenceScreen>
And that's how I switch to SettingsActivity
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
Is white screen is supposed to happed? I'm expecting a default screen with settings
I found the solution.
The screen wasn't blank, my theme made all the text white on white background.
So I just added a custom theme for settings activity and added this to android manifest:
android:theme="#style/SettingsTheme"
I have MenuSettings in which I have PreferenceActivity class. In PreferenceActivity I have displayed checkboxPreference like below.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="Options">
<CheckBoxPreference
android:key="pref_opt1"
android:title="Option 1"
android:summary="Tick to set this option"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="pref_opt2"
android:title="Option 2"
android:summary="Tick to set this option"
android:defaultValue="true"
/>
</PreferenceCategory>
</PreferenceScreen>
I want to use checkboxes to perform function for synchronization that I have build, But Please can any body let me know how to use checkboxes in PreferenceActivity. Is there any need to create another Activity? or we can perform function in same PreferenceActivity?
here is my PreferenceActivity:
public class SharedPref extends PreferenceActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.sharedpref);
}
// StorePreference method{
}
// readPreferences method{
}
}
I have a PreferenceActivity where I represent a setting UI, when I press the first checkbox then the second checkbox becomes visible. On the other hand, when the first checkbox is unchecked the second checkbox is hidden(and gets a default value to false).
The problem is that I need my UI screen to be refreshed the moment I check(or uncheck) the first checkbox because right now with that code I need to go to another Activity (e.g when I press the back button) and then get back to the PreferenceActivity again in order to see the changes on my UI.
Code for PreferenceActivity:
public class SetPreference extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.checkboxpref);
}
#Override
protected void onStart() {
super.onStart();
Preference checkbox = findPreference("checkBox_Schedule");
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
if (prefs.getBoolean("checkBox", true)) {
} else {
((PreferenceGroup) findPreference("category_second"))
.removePreference(checkbox);
SharedPreferences.Editor geted = prefs.edit();
geted.putBoolean("checkBox_Schedule", false);
geted.commit();
}
}
#Override
protected void onResume() {
super.onResume();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
}
}
Code for XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="Username and password information"
android:title="Login information" >
<EditTextPreference
android:key="username"
android:summary="Please enter your login username"
android:title="Username" />
<EditTextPreference
android:inputType="textPassword"
android:key="password"
android:summary="Enter your password"
android:title="Password" />
</PreferenceCategory>
<PreferenceCategory
android:summary="Username and password information"
android:title="Settings"
android:key="category_first" >
<CheckBoxPreference
android:key="checkBox"
android:summary="On/Off"
android:title="Keep me logged in" />
<ListPreference
android:entries="#array/listOptions"
android:entryValues="#array/listValues"
android:key="listpref"
android:summary="List preference example"
android:title="List preference" />
</PreferenceCategory>
<PreferenceCategory
android:summary="schedule"
android:key="category_second"
android:title="Schedule" >
<CheckBoxPreference
android:key="checkBox_Schedule"
android:summary="On/Off"
android:title="Keep me logged in" />
</PreferenceCategory>
</PreferenceScreen>
Thank you for your time.
Ok then just restart the activity when the checkbox is selected/deselected:
startActivity(new Intent(PreferencesActivity.this, PreferencesActivity.class));
finish();
Consider using onPreferenceSelectedListener for the first checkbox to detect the act of checking/unchecking the preference.
I have a preferences page in my application. As there is no Multiple Choice ListPreference (There's one after API Level 11) I want to put a ListView at preferences page. But the preferences.xml doesn't let me to insert a Linear Layout.
Here's my preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Auto Refresh Settings">
<CheckBoxPreference
android:title="Auto Refresh"
android:defaultValue="false"
android:summary="Enable / Disable Auto Refresh"
android:key="checkboxPref" />
<ListPreference
android:title="Auto Refresh Frequency"
android:summary="Select the frequency of Auto Refresh"
android:key="listPref"
android:defaultValue="20"
android:entries="#array/listArray"
android:entryValues="#array/listValues" />
</PreferenceCategory>
</PreferenceScreen>
This is how it looks:
What I want:
:
Lastly my Settings.java (preferences):
package com.sarkolata.coding;
import android.content.Context;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.widget.Toast;
public class Settings extends PreferenceActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
final Preference ListPref = (Preference) findPreference("listPref");
final Preference CheckPref = (Preference) findPreference("checkboxPref");
if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getBoolean("checkboxPref", false)) {
ListPref.setEnabled(false);
}
ListPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Main.update_tick = Integer.parseInt(newValue.toString()) * 1000;
return true;
}
});
CheckPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
if(newValue.toString() == "true")
{
ListPref.setEnabled(false);
} else {
ListPref.setEnabled(true);
}
if(newValue.toString() == "true") {
Main.refreshAllServers(Main.context, Main.bcontext,"start");
} else {
Main.refreshAllServers(Main.context, Main.bcontext,"stop");
}
return true;
}
});
}
}
I don't know if is is clever to add a ListView inside a preference pane because the PreferenceActivity already uses a ListView to show the many preferences. Perhaps you want to use a MultiSelectListPreference, preference that allows you to do multiple selection. You can see an example at http://blog.350nice.com/wp/archives/240