In my Android application, I have implemented class SubtitleColorListPreference which extends from ListPreference. I need this, because I need to set my own layout for each item in list. Everything works fine and it looks like this:
The important code is in method onPrepareDialogBuilder(AlertDialog.Builder builder), where I set my own ListAdapter.
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
// some other code is here ...
ListAdapter listAdapter = new SubtitleColorAdapter(getContext(), R.layout.subtitle_color_preference_item, colorNameHolders, index, this);
builder.setAdapter(listAdapter, this);
}
Now, I need to use PreferenceFragmentCompat instead of PreferenceFragment, so my SubtitleColorListPreference has to extend android.support.v7.preference.ListPreference. And here is the problem - there are no onPrepareDialogBuilder method in this ListPreference. Also, I did not find any similar method. I tried to find some examples how to create custom android.support.v7.preference.ListPreference, but with no success.
Does anybody know how can I solve this?
Google thinks that you don't need a custom view in ListPreference. It seems, the only way is to use the deprecated PreferenceFragment and android.preference.ListPreference or stay with obsolete SDK 27. It's clear that they don't understand what they're doing.
Related
I'm working on a little app with a lot of modifiable preferences, most of them being SeekBarPreferences.
It happens that, since I'm quite not happy with Android default SeekBarPreferences, I'm using the very good MaterialSeekBarPreference library which unfortunately have not been updated for 2 years.
Here is an example of code used by this library:
<com.pavelsikun.seekbarpreference.SeekBarPreference
android:key="#string/param_maxEvent"
android:title="Blahlblahblahblahblah"
android:summary="Blahlblahblahblahblah too"
android:defaultValue="2"
custom:msbp_minValue="0"
custom:msbp_maxValue="5"
custom:msbp_measurementUnit="events"
custom:msbp_interval="1"
custom:msbp_dialogEnabled="false"/>
As you can see, you can use the android:defaultValue xml attribute, and it works perfectly with the UI.
Since I need to load all these default values at app initialization, I use the PreferenceManager.setDefaultValues method:
public class App extends Application {
#Override public void onCreate() {
super.onCreate();
PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
}
}
This works fine with all default preferences (SwitchPreference, ListPreference, Preference), but unfortunately not with these custom SeekBarPreference.
Loading the preferences activity does not set up thoses default values either.
Is there any workaround for this problem ? Else, if I was up to edit the library, what should I change ?
I don't use the method PreferenceManager.getDefaultSharedPreferences(this, R.xml.preferences, true);. Instead, I use preference.setDefaultValue(object); in the Fragment.
I've been having trouble implementing the animate additions functionality of nhaarman's ListViewAnimations library.
As specified in the wiki, I'm using the DynamicListView, and my ArrayAdapter implements Insertable.
However, things aren't working. A couple of questions:
Do I use a normal ArrayAdapter or do I have to use the library's ArrayAdapter? I've tried both. I suspect the correct answer is to use a normal ArrayAdapter and override the following function:
public void add(int i, #NonNull Comment comment)
If that's the case, what do I have to put inside this function? notifyDataSetChanged()?
Do I have to wrap my adapter in of the AnimationAdapters?
I repair big android app and I have many custom dialogs which inherit from various types of standard android dialogs (AlertDialog, ProgressDialog, ...).
I have to add option "setCanceledOnTouchOutside(false)" for all dialogs in app, because in ICS we have this option set on true by default android 4.0 Dialog gets canceled when touched outside of dialog window
I can add line "*dialog.setCanceledOnTouchOutside(false)" for every dialog in my project, but this is hard to maintanence solution.
I can't inherit from MyDialog which inherit from Dialog, because I inherit from AlertDialog, ProgressDialog,... too.
Probably the best solution would be set all dialogs option for whole project in one place or make any hack that give us by default behavior from older android version than ICS, but I don't know if it is possible and how to do this?
Can You advise me?
have all the dialogs implement a common interface, like this,
interface DialogSettings {
void setCancelOnTouchOutside(boolean cancel);
}
now implement a stand-alone version of this,
class VersionAwareDialogSettings implements DialogSettings {
private final Dialog d;
DialogSettingsImpl(Dialog d) {
this.d = d;
}
#Override
void setCancelOnTouchOutside(boolean cancel) {
if (Build.Version.SDK_INT ...) {
d.setCancelOnTouchOutside(cancel);
}
}
}
now, in all your dialog classes, implement this interface, like this,
class MyDialog extends AlertDialog implement DialogSettings {
...
#Override
public void setCancelOnTouchOutside(boolean cancel) {
new VersionAwareDialogSettings(this).setCancelOnTouchOutside(cancel);
}
}
this can seem like a lof of cruft, but it's a nice separation of concerns. if you want to add a version-aware dialog setting, change the interface, and all parts of your code that you need to modify are flagged. if you need to change the impl of a verison-aware dialog setting, just change it in one place: VersionAwareDialogSettings.
you should be more concerned with writing correct code than trying to cut down on the number of lines you write. i don't know about you, but i can type much faster than i think. my brain is the bottleneck.
The Settings app in Android has the following source code:
Code
The Settings class derives from the PreferenceActivity. Even with the android source code, I am not able to figure out what code in the class is responsible for displaying the difference options in the settings screen.
My guess is that the right code is in onResume():
public void onResume() {
super.onResume();
ListAdapter listAdapter = getListAdapter();
if (listAdapter instanceof HeaderAdapter) {
((HeaderAdapter) listAdapter).resume();
}
}
Can someone help?
Thanks.
I am not able to figure out what code in the class is responsible for displaying the difference options in the settings screen.
That is mostly in the preference XML, such as this file that declares the preference headers (left column on a tablet's view of Settings). Some preferences are implemented via custom PreferenceFragments, where the UI departs from the preference norms.
EDIT 2:
I did solve my problem, but i don't know how:S I was moving my code snippets a bit around, a suddenly it worked. Must have done something in the wrong order, but its weird, checked it many times. Thanks for you help, and sorry I can't post an answer ;)
Hi.
I have a list view which I'm trying to refresh to update it self when i add an element to
the underlying array list.
Here is the code snippet:
private void addEvent() {
arrlEvents.add( event );
adptEvents.notifyDataSetChanged();
updateSaveFile();
filterList();
}
The arrlEvents is the underlying arraylist with the events, and im adding one event, trying to update the list view with notifyDataSetChanged(), but it doesnt work. Can anyone help?
Thanks for your time:)
EDIT:
Here is the source code for the adapter:
private ArrayAdapter<Event> adptEvents;
adptEvents = new ArrayAdapter<Event>( EventCalendar.this, R.layout.list_items, arrlEvents );
I have seen that sometimes it just randomly doesnt notify the adapter.
Try using adptEvents as protected or public on a global scope.
I have found that when that doesnt work. You can just re set the adapter again, just substitute the notifyDataSetChanged() for:
adptEvents = new ArrayAdapter<Event>( EventCalendar.this, R.layout.list_items, arrlEvents );
Edit:
Heres a code snipper from an App I wrote that works.
Class definition:
public class ClassName extends ListActivity implements AdapterView.OnItemSelectedListener {
Global Variable:
CustomAdapter adapter;
in OnCreate():
adapter = new CustomAdapter(this,R.layout.layout_name,dataSet);
setListAdapter(adapter);
Whenever I need to notify
adapter.notifyDataSetChanged();
There is no persistent link between arrlEvents and the adptEvents.... the latter simply initialises itself with the elements from the former. adptEvents has no way to know when arrlEvents changes.
To add new items you should call adptEvents.add(event) and not bother calling notifyDataSetChanged() explicitly, since ArrayAdapter.add() does that for you automatically.