Cannot get a hold on my Preferences with Unified Preferences - android

I use this library to create my PreferenceActivity. It works quite fine so far but I can not set any listeners on my Preference.
That how my Activity looks like:
public class PrefActivity extends UnifiedSherlockPreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
setHeaderRes(R.xml.preference_headers);
// Set desired preference file and mode (optional)
setSharedPreferencesMode(Context.MODE_PRIVATE);
super.onCreate(savedInstanceState);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Preference p = (Preference)findPreference("deleteSavedSearches");
p.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
//code for what you want it to do
return true;
}
});
}
}
This is my PreferenceScreen:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference
android:key="deleteSavedSearches"
android:summary="Gespeicherte Suchen löschen"
android:title="Gespeicherte Suchen löschen" />
</PreferenceScreen>
And those are my headers:
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:unified="http://schemas.android.com/apk/res-auto" >
<!--
/*
** Header definitions are identical to android:* except for preferenceRes which should be a reference to a preference xml file
** Unlike the native headers these are also used for building the single pane version.
*/
-->
<header
unified:fragment="com.example.skelett.PrefActivity$GeneralPreferenceFragment"
unified:preferenceRes="#xml/pref_country"
unified:title="Land" />
<header
unified:fragment="com.example.skelett.PrefActivity$GeneralPreferenceFragment"
unified:preferenceRes="#xml/pref_data"
unified:title="Daten" />
</preference-headers>
In this case, p is null. getPreferenceScreen() also always returns null. I have my keys set up in the preference xml. What am I missing?

Just move the findPreference method in onPostCreate and should work:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Preference p = (Preference) findPreference("deleteSavedSearches");
p.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
// code for what you want it to do
return true;
}
});
}

Related

android.preference.Preference.setOnPreferenceClickListener() does not work

I want to set an intent from my SettingsFragment (extends PreferenceFragment) to another activity within my app. I tried using the following code at first: (I'm using android.preference.Preference)
preferences.xml (the root preferences)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
...
<PreferenceCategory android:title="About">
<Preference
android:key="about_here"
android:title="About us"
android:enabled="true" />
</PreferenceCategory>
</PreferenceScreen>
Then, in my SettingsFragment.java:
public class SettingsFragment extends PreferenceFragment {
public static final String goToAbout = "about_here";
private ViewGroup parent;
private View currView;
#Override
public void onCreate(#Nullable Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Preference preferencex = findPreference(goToAbout);
preferencex.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
Intent i = new Intent(getActivity(), AboutActivity.class);
i.putExtra("Class", "Settings");
preference.setIntent(i);
Log.e("Preference", "Clicked");
getActivity().startActivity(i);
return true;
}
});
}
...
}
However, the onPreferenceClick code is not invoked at all (I know from the Log.e() that it does not happen). I know that many have asked this question before, but even implementing their solutions does not help. Is there anything I'm missing?
Also, I don't mind implementing an Intent from within preferences.xml, but I tried that and it returns an error.

How do I open android Text to speech settings from preference screen?

I am trying to open the android default TTS settings whenever I click on the particular preference in my App settings. My pref_settings.xml looks like this:
<PreferenceScreen
android:key="Lang_Select"
android:title="Language"
android:summary="Select a Language">
</PreferenceScreen>
This is my list in android settings. and my SettingsActivity.java looks like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
public static class ChatSettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.pref_settings);
}
}
#Override
public void onBackPressed() {
super.finish();
}
}
How can I start the android default TTS settings whenever the language button is clicked?
Thanks.
I have not worked with PreferenceFragmentCombat only with normal PreferenceFragment, but I guess it´s not that much difference in the basic implementations. So please, be noticed that this is maybe not the answer for your problem, but I try to help you a little bit and have to show some code. I have done it in my onCreate() method like this:
public class PreferenceFragment extends android.preference.PreferenceFragment {
private Preference mYourPreference;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_layout);
//initialize your preference with the key you used in xml layout
mYourPreference=(Preference)getPreferenceManager().findPreference(yourPreferenceKey);
//set on click listener
mYourPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
//start the activity
startActivity(new Intent(android.provider.Settings.ACTION_VOICE_INPUT_SETTINGS), 0);
return true;
}
});
}
}
I don´t know if ACTION_VOICE_INPUT_SETTINGS will work, just try it.
Try this:
startActivity(Intent("com.android.settings.TTS_SETTINGS"))
See: https://cs.android.com/android/platform/superproject/+/master:cts/apps/CtsVerifier/src/com/android/cts/verifier/speech/tts/TtsTestActivity.java;l=21?q=TTS_SETTINGS

Android preferences how to work with switch statement

I have some preferences in my preference screen:
<PreferenceCategory
android:key="category"
android:summary="Category"
android:title="Category">
<Preference
android:key="pref1"
android:summary="desc"
android:title="Pref 1" />
<Preference
android:key="pref2"
android:summary="desc"
android:title="Pref 2" />
</PreferenceCategory>
Finding them in PreferenceActivity:
Preference pref1, pref2;
#Override
protected void onCreate(final Bundle savedInstanceState) {
pref1 = findPreference("pref1");
pref2 = findPreference("pref2");
}
And set some OnPreferenceClickListener to them. How do I correctly define which preference was clicked? I'd like to do it in case-switch style, but I cannot figure out which types should I use:
Preference.OnPreferenceClickListener listener = new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
switch (???){ //I guess, here goes the "preference"?
case ???: //getting error with pref1 or pref2
}
return false;
}
}
If I put preference from onPreferenceClick in switch(), I will get errors with case.
You can get the corresponding preference like
#Override
public boolean onPreferenceClick (Preference preference)
{
String key = preference.getKey();
// do what ever you want with this key
}
ref: Preference Activity on Preference Click Listener
hope this helps :)
You can use the field key of Preference
public boolean onPreferenceClick(Preference preference) {
if (preference.getKey().equals("pref1")) {
... do something ...
} else if (preference.getKey().equals("pref2")) {
... do something ...
}
return true;
}

setOnPreferenceClickListener function doesn't work for Preference on Android

I'm using the code below to print text to logcat, but the setOnPreferenceClickListener function doesn't catch the event.
I'm using Android API Level 8 to test the code.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:defaultValue="true"
android:key="settings_use_cache"
android:summary="Use cache"
android:title="Use Cache" />
<Preference
android:defaultValue="true"
android:key="settings_delete_cache"
android:summary="Delete all cache data"
android:title="Clear Cache" />
</PreferenceScreen>
Here is the code
public static class CachePreferenceFragment extends
PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
Log.w("DBG", "Oncreate started");
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_cache);
Preference settings_delete_cache=findPreference("settings_delete_cache");
settings_delete_cache.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
Log.w("Prefence", "Deleting Cache");
return false;
}
});
}
}
What can I do to ensure the listener catches the event?
EDIT:
After talking with this dev outside of the thread, it was identified that the problem was that he was putting his listener inside of the fragment. So this code actually works, but it will only run if you are using a two-panel layout. In fact this is stated right above the method:
/**
* This fragment shows notification preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
public static class CachePreferenceFragment extends
PreferenceActivity {
The solution is to also set the onPreferenceClick listener in setupSimplePreferencesScreen()!

Preference Screen option is not showing

My Preference Screen option is not showing, it shows-app. has stopped unexpectedly..
This is my Preference.java-
public class Prefs extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.id.settings);
}
}
This is the settings for Preference-
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="music"
android:title="#string/music_title"
android:summary="#string/music_summary"
android:defaultValue="true"/>
<CheckBoxPreference
android:defaultValue="true"
android:summary="#string/hints_summary"
android:title="#string/hints_title"
android:key="hints"/>
</PreferenceScreen>
This is the Item Select event-
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.settings)
{
startActivity(new Intent(this,Prefs.class));
return true;
}
return false;
}
And the activity is registered well in manifest file.
<activity
android:name=".Prefs"
android:label="#string/settings_title" >
</activity>
addPreferencesFromResource()
method should load XML file containing the preferences.
Therefore, in you code replace
addPreferencesFromResource(R.id.settings)
with
addPreferencesFromResource(R.xml.yourPreferenceSettingsFileHere)
That will solve your problem.
please replace addPreferencesFromResource(R.id.settings); with addPreferencesFromResource(R.layout.settings);
where settings is the preference xml.
and instead of using onOptionsItemSelected()
use
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
final String key = preference.getKey();
if(key.equal("music"){
/ur implementaion
}
}**

Categories

Resources