So, I have this xml for PreferenceScreen
.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:dialogMessage="@string/w_common_display_name_desc"
android:key="display_name"
android:summary="@string/w_common_display_name_desc"
android:title="@string/w_common_display_name" />
<EditTextPreference
android:dialogMessage="@string/w_basic_username_desc"
android:inputType="textVisiblePassword"
android:key="username"
android:summary="@string/w_basic_username_desc"
android:title="@string/w_basic_username" />
<EditTextPreference
android:dialogMessage="@string/w_common_server_desc"
android:inputType="textVisiblePassword"
android:key="server"
android:summary="@string/w_common_server_desc"
android:title="@string/w_common_server" />
<com.csipsimple.widgets.PasswordPreference
android:dialogMessage="@string/w_basic_password_desc"
android:key="password"
android:password="true"
android:summary="@string/w_basic_password_desc"
android:title="@string/w_basic_password" />
</PreferenceScreen>
That is called by this PreferenceActivity
.
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.text.TextUtils;
import com.actionbarsherlock.view.Menu;
import com.csipsimple.R;
import com.csipsimple.utils.Log;
public abstract class GenericPrefs extends PreferenceActivity implements
OnSharedPreferenceChangeListener, IPreferenceHelper {
private static final String THIS_FILE = "GenericPrefs";
private static String TAG = "ricky";
public abstract boolean onCreateOptionsMenu(Menu menu);
/**
* Get the xml preference resource for this screen
*
* @return the resource reference
*/
protected abstract int getXmlPreferences();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
beforeBuildPrefs();
addPreferencesFromResource(getXmlPreferences());
afterBuildPrefs();
Log.d(TAG, "GenericPrefs");
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
updateDescriptions();
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
updateDescriptions();
}
/**
* Process update of description of each preference field
*/
protected abstract void updateDescriptions();
/**
* Optional hook for doing stuff before preference xml is loaded
*/
protected void beforeBuildPrefs() {
// By default, nothing to do
}
/**
* Optional hook for doing stuff just after preference xml is loaded
*/
protected void afterBuildPrefs() {
// By default, nothing to do
}
// Utilities for update Descriptions
/**
* Get field summary if nothing set. By default it will try to add _summary
* to name of the current field
*
* @param field_name Name of the current field
* @return Translated summary for this field
*/
protected String getDefaultFieldSummary(String field_name) {
try {
String keyid = R.string.class.getField(field_name + "_summary").get(null).toString();
return getString(Integer.parseInt(keyid));
} catch (SecurityException e) {
// Nothing to do : desc is null
} catch (NoSuchFieldException e) {
// Nothing to do : desc is null
} catch (IllegalArgumentException e) {
// Nothing to do : desc is null
} catch (IllegalAccessException e) {
// Nothing to do : desc is null
}
return "";
}
/**
* Set summary of a standard string field If empty will display the default
* summary Else it displays the preference value
*
* @param fieldName the preference key name
*/
public void setStringFieldSummary(String fieldName) {
PreferenceScreen pfs = getPreferenceScreen();
SharedPreferences sp = pfs.getSharedPreferences();
Preference pref = pfs.findPreference(fieldName);
String val = sp.getString(fieldName, null);
if (TextUtils.isEmpty(val)) {
val = getDefaultFieldSummary(fieldName);
Log.d(TAG, "genericPrefs field summary: "+val);
}
setPreferenceSummary(pref, val);
}
/**
* Set summary of a password field If empty will display default summary If
* password will display a * char for each letter of password
*
* @param fieldName the preference key name
*/
public void setPasswordFieldSummary(String fieldName) {
PreferenceScreen pfs = getPreferenceScreen();
SharedPreferences sp = pfs.getSharedPreferences();
Preference pref = pfs.findPreference(fieldName);
String val = sp.getString(fieldName, null);
if (TextUtils.isEmpty(val)) {
val = getDefaultFieldSummary(fieldName);
} else {
val = val.replaceAll(".", "*");
}
setPreferenceSummary(pref, val);
}
/**
* Set summary of a list field If empty will display default summary If one
* item selected will display item name
*
* @param fieldName the preference key name
*/
public void setListFieldSummary(String fieldName) {
PreferenceScreen pfs = getPreferenceScreen();
ListPreference pref = (ListPreference) pfs.findPreference(fieldName);
if (pref == null) {
Log.w(THIS_FILE, "Unable to find preference " + fieldName);
return;
}
CharSequence val = pref.getEntry();
if (TextUtils.isEmpty(val)) {
val = getDefaultFieldSummary(fieldName);
}
setPreferenceSummary(pref, val);
}
/**
* Safe setSummary on a Preference object that make sure that the preference
* exists before doing anything
*
* @param pref the preference to change summary of
* @param val the string to set as preference summary
*/
protected void setPreferenceSummary(Preference pref, CharSequence val) {
if (pref != null) {
pref.setSummary(val);
}
}
/**
* Hide a preference from the screen so that user can't see and modify it
*
* @param parent the parent group preference if any, leave null if
* preference is a root pref
* @param fieldName the preference key name to hide
*/
public void hidePreference(String parent, String fieldName) {
PreferenceScreen pfs = getPreferenceScreen();
PreferenceGroup parentPref = pfs;
if (parent != null) {
parentPref = (PreferenceGroup) pfs.findPreference(parent);
}
Preference toRemovePref = pfs.findPreference(fieldName);
if (toRemovePref != null && parentPref != null) {
parentPref.removePreference(toRemovePref);
} else {
Log.w("Generic prefs", "Not able to find" + parent + " " + fieldName);
}
}
@Override
public void setPreferenceScreenType(String key, int type) {
setPreferenceScreenType(getClass(), key, type);
}
@Override
public void setPreferenceScreenSub(String key, Class<?> activityClass, Class<?> fragmentClass, int type) {
setPreferenceScreenType(activityClass, key, type);
}
private void setPreferenceScreenType(Class<?> classObj, String key, int type) {
Preference pf = findPreference(key);
Intent it = new Intent(this, classObj);
it.putExtra(PrefsLogic.EXTRA_PREFERENCE_TYPE, type);
pf.setIntent(it);
}
/* (non-Javadoc)
* @see android.preference.PreferenceActivity#isValidFragment(java.lang.String)
*/
public boolean isValidFragment(String fragmentName) {
// This pref activity does not include any fragment
return false;
}
}
I looked at a lot tutorials about PreferenceScreen
and the xml looks fine. This is part of a project that used SherlockFragment
before and I'm currently upgrading some parts of it's code. When I compile the app, the summary
doesn't show up for some reason. The dialogMessage
gets displayed just fine.
I already added this on my AndroidManifest
.
implementation 'com.android.support:preference-v7:28.0.0'
I found out today that the ListView
that is used by the code is not showing anything even in the layout editor. I tried replacing it with RecyclerView
but it throws an error Caused by: java.lang.ClassCastException: android.support.v7.widget.RecyclerView cannot be cast to android.widget.ListView
. I looked for anything like a findViewById
but can't find any. I'd rather deal with the not showing anything error than that because I feel like it is more complex than the former.
Here's the xml used for displaying the PreferenceScreen
.
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2010 Regis Montoya (aka r3gis - www.r3gis.fr)
This file is part of CSipSimple.
CSipSimple is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
If you own a pjsip commercial license you can also redistribute it
and/or modify it under the terms of the GNU Lesser General Public License
as an android library.
CSipSimple is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CSipSimple. If not, see <http://www.gnu.org/licenses/>.
-->
<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:orientation="vertical">
<LinearLayout
android:id="@+id/settings_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/validation_bar"
android:orientation="vertical">
<LinearLayout
android:id="@+id/custom_wizard_row"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="@+id/custom_wizard_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:drawable/divider_horizontal_dark"
android:contentDescription="@string/empty_description" />
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
tools:listitem="@layout/adptr_simple"
android:layoutAnimation="@anim/layout_slide_right"
android:persistentDrawingCache="animation|scrolling" />
</LinearLayout>
<LinearLayout
android:id="@+id/validation_bar"
style="@style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/cancel_bt"
style="@style/ButtonBarButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/cancel" />
<Button
android:id="@+id/save_bt"
style="@style/ButtonBarButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/save" />
</LinearLayout>
</RelativeLayout>
I tried changing the Theme
but it still doesn't show the text. I tried removing android:layoutAnimation
and android:persistentDrawingCache
but it still doesn't show the summary
.
from PreferenceScreen not displaying summary after updating app
No comments:
Post a Comment