Switch to a nested PreferenceScreen from activity - android

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?

Related

How to set ToolBar into PreferenceScreen in android

I want use PreferenceScreen for my application setting, but i want show this Preferences below the ToollBar.
toolbar.xml code :
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:navigationContentDescription="#string/abc_action_bar_up_description"
android:background="?attr/colorPrimary"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="Setting Page"
/>
setting_preferences.xml (path is : res/xml) code :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="setting_title_title_category"
android:title="Title options">
<CheckBoxPreference
android:id="#+id/setting_title_show_id"
android:key="setting_title_show"
android:title="Show Main Title"
android:summary="Show/hide MainPage title"
android:checked="true"/>
<EditTextPreference
android:key="setting_title_text"
android:title="Set Main Title"
android:summary="Change MainPage title"
android:dialogTitle="Change Title"
android:dialogMessage="Change title please..."/>
</PreferenceCategory>
<PreferenceCategory
android:key="setting_title_font_category"
android:title="Font options">
<ListPreference
android:key="setting_title_font_color"
android:title="Title font colors"
android:summary="Change title font colors"
android:entries="#array/colors"
android:entryValues="#array/colors"
android:dialogTitle="Change font color" />
</PreferenceCategory>
</PreferenceScreen>
SettingPage code :
public class SettingPage extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("setting_title_font_color")) {
// get preference by key
Preference pref = findPreference(key);
// do your stuff here
}
if (key.equals("setting_title_show")){
Preference pref = findPreference(key);
}
}
public static class MyPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.setting_prefrences);
}
}
How can it? show setting_preferences.xml beloe ToolBar .
I believe you can use a normal Activity / AppCompatActivity with a simple layout that contains the Toolbar. Then in the content area of the Activity layout, use a PreferenceFragment to show the PreferenceScreen.

How to set PreferenceScreen into other layout in android

I want use PreferenceScreen in my application, and create XML layout for this activity in res/xml/setting_preference.xml. but this XML layout is very simple, i want use this layout in another layout.
For example : i want use setting_preference.xml into setting_activity.xml , setting_activity.xml path is res/layout/setting_activity.xml.
How can i it?
setting_preference XML code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="setting_title_title_category"
android:title="Title options">
<CheckBoxPreference
android:id="#+id/setting_title_show_id"
android:key="setting_title_show"
android:title="Show Main Title"
android:summary="Show/hide MainPage title"
android:checked="true"/>
<EditTextPreference
android:key="setting_title_text"
android:title="Set Main Title"
android:summary="Change MainPage title"
android:dialogTitle="Change Title"
android:dialogMessage="Change title please..."/>
</PreferenceCategory>
<PreferenceCategory
android:key="setting_title_font_category"
android:title="Font options">
<ListPreference
android:key="setting_title_font_color"
android:title="Title font colors"
android:summary="Change title font colors"
android:entries="#array/colors"
android:entryValues="#array/colors"
android:dialogTitle="Change font color" />
</PreferenceCategory>
<RingtonePreference
android:title="tes"/>
</PreferenceScreen>
setting_activity XML code:
<?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"
tools:context="com.tellfa.nikandroid.mytestdbproject.SettingPage">
</RelativeLayout>
Setting Page code:
public class SettingPage extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("setting_title_font_color")) {
// get preference by key
Preference pref = findPreference(key);
// do your stuff here
}
if (key.equals("setting_title_show")){
Preference pref = findPreference(key);
}
}
public static class MyPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.setting_prefrences);
}
}
}
Add a frameLayout where you want to load your xml preference file in the layout xml file and implement PreferenceFragment in that activity.

addPreferencesFromResource

I know this might be a duplicate from: What to use instead of "addPreferencesFromResource" in a PreferenceActivity?
But I still can not get the code to work and would appreciate some help. I can not get the:
"addPreferencesFromResource(R.xml.preferences);" to work.
preferences is not resolved, even though it is in the R.xml folder.
I followed the example in the above link to the letter.
Please help!!
package com.example.oasisreference;
import android.R;
import android.R.xml;
//import android.annotation.TargetApi;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class Prefs extends PreferenceActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
preferences.xml in the Res.xml folder
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:title="User Name"
android:key="name"
android:summary="Enter your name"
></EditTextPreference>
<CheckBoxPreference
android:title="Euro Currency"
android:defaultValue="false"
android:key="euro"
android:summary="Check for Use Cost Calculator to be in Euros/Liters">
</CheckBoxPreference>
</PreferenceScreen>
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/aboutUs"
android:title="About the Author">
</item>
<item
android:id="#+id/preferences"
android:title="Preferences">
</item>
<item
android:id="#+id/exit"
android:title="Exit">
</item>
</menu>
delete import android.R
it will work
The answer is, it's not actually deprecated....
They just want you to use preferenceFragments (see below)
It turns out that it's the same routine name but it is instantiated as part of a preferenceFragment which is a different class. There's it's not deprecated. This is subtle but the examples at the developer website reference it from within a preference fragment. Paste the deprecated call into a preferenceFragment class and see for yourself the deprecation goes away
In the preferenceFragment class declaration:
package android.preference;
public abstract class PreferenceFragment extends android.app.Fragment {
// ...
public void addPreferencesFromIntent(android.content.Intent intent) {
/* compiled code */
}
  
public void addPreferencesFromResource(int preferencesResId) {
/* compiled code */
}
// ...
and in the other class
package android.preference;
public abstract class PreferenceActivity extends android.app.ListActivity implements android.preference.PreferenceFragment.OnPreferenceStartFragmentCallback {
/**
* #deprecated
*/
#java.lang.Deprecated
public void addPreferencesFromResource(int preferencesResId) {
/* compiled code */
}
Add #SuppressWarnings("deprecation") before OnCreate method.

Android - Placing a ListView in Preferences Page

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

Preference ListView doesn't populate

I have a custom Preference consisting in a ListView. I want to show it in a PreferenceActivity (in the same screen not in a Diaog) but it doesn't populate. I show you da code, lol. My ListPreferences object it's only an object that extends from Preference
PreferenceActivity:
public class SettingsActivity extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
preference.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:key="themeTitle"
android:title="#string/settings_group_themes">
<com.battleship.preferences.ListPreferences
android:key="theme" android:widgetLayout="#id/listView" />
</PreferenceCategory>
</PreferenceScreen>
list_preference.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView" android:layout_width="wrap_content"
android:choiceMode="singleChoice" android:layout_height="wrap_content"
android:orientation="vertical" android:footerDividersEnabled="true"
android:entries = "#array/themeArray"
/>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="themeArray">
<item>Default</item>
<item>Black</item>
<item>White</item>
</string-array>
</resources>

Categories

Resources