Adding an item to listpreference - java

I have a preference screen containing a preferenceList. i want the user to be able to add new friends to that list.
I have been unable to find any examples of how i can achieve such a thing doing run time.
Does anyone have an example or able to give me an example of how i would achieve this?
Here is my preference:
<PreferenceCategory android:title="Your competetion">
<PreferenceScreen
android:key="secondPrefScreen"
android:summary="click here to see your friends"
android:title="Friends" >
<ListPreference
android:key="friendList"
/>
</PreferenceScreen>

You can access the ListPreference object at runtime and use the setter and getter methods to update the entries. ListPreference Docs
Assuming you are inside a PreferenceActivity:
PreferenceScreen root = this.getPreferenceScreen();
ListPreference list = (ListPreference) root.findPreference("secondPrefScreen");
// do stuff with list

Related

Custom callback function attribute android

As android defines attributes inside the attrs files and I have seen that Button has attribute onClick listed. Is there a way to create custom attributes referencing functions as so and how does on go about implementing it.
<Button
...
android:onClick="onClickFunction"/>
What I want to have
<CustomView
...
android:onItemClick="onItemClicked"/>
attrs.xml
<attr name="onItemClick" format="string"/>
How does one create attach this function as a listener to the view?

#android:id is not recognized by eclipse

In my xml file I have below list view.
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:divider="#color/blue"
android:dividerHeight="2dip"
android:background="#drawable/list_divider"
>
</ListView>
And in my java class I try to access the view as follows
ListView myListView = (ListView) findViewById(R.id.list);
I get a error for this saying "list cannot be resolved or is not a field". But if I change the id as android:id="#+id/list"
How can I access the view by using the id this way android:id="#android:id/list".
Also whats the basic difference between two ways of defining ID. Thanks in advance
Assuming your Activity extends ListActivity, you can access it with the convenience method
ListView myListView = getListView();
otherwise you would access it with android.R.id.list
Also whats the basic difference between two ways of defining ID
#android:id references built-in android resources
#+id is a user defined id and will add it to the R.java file to be accessed later with #id
See this post about more on that
You have to mention your id like
android:id="#+id/list"
#+id/list will create a resource ID in your app (=your package) with the name "list" and give it a unique ID. In code, that would be R.id.list.
#android:id/list will use the ID "list" from the package android (which, in code, would be android.R.id.list.
Android resources id are referenced by #android:id/list in the xml
and android.R.id.list in the Java code.
User defined id are referenced by #+id/list in the xml and
R.id.list in the Java code. The + implies that the resource id value will be generated at compile time.
id define in resource files as <item type="id" name="list" /> will
be referenced as #id/list in the xml and R.id.list in the Java
code.

Java move <Preference> from one PreferenceScreen to another?

I'm trying to do something similar to iOS editable UITableView. I have a list of "added items" if the user clicks on "Add new item" he is taken to a PreferenceScreen with multiple <Preference> elements, onclick the should disappear from this screen and add it to the "Added list" (actually a <PreferenceCategory>), then they should be able to remove them from here or edit their order. This is similar to iOS editable lists.
This is my XML:
<PreferenceScreen android:title="ALL APPS" android:key="app_list">
<!-- A <preference> for each app -->
</PreferenceScreen>
<PreferenceCategory android:title="ADDED APPS" android:key="my_apps_list">
</PreferenceCategory>
I have tried uncountable times and I cannot get it to work.
How can I get it done?
I've experimented around a bit and it's actually very easy when you nest your PreferenceScreens.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference android:title="Check it!" />
<PreferenceScreen android:title="To the other preferences!" android:summary="I really hope this works <3" >
<EditTextPreference android:title="Edit your name" android:summary="Edit your name :)" />
<CheckBoxPreference android:title="Accept answer" />
</PreferenceScreen>
</PreferenceScreen>
PS: Please excuse my hard-coded string literals - it was just for testing! :)
this looks like a very similar question to what i've asked in the past:
android - showing listView in PreferenceActivity
when selecting the items to show , store them in some way (DB ,references ,etc...) and then use the solution i've found .
as you can see , the view used in preference activity is actually a listView , so it's weird to put a listView in a listView . what you need is a special trick , and that's something i've written about .
Please have a look to the accepted answer on this question:
How to remove Android preferences from the screen
Sometimes, if categories are present, you have to
"reference the category containing the preference you wanted to remove and then remove that preference from that category"
like the answer there says. Isn't this also your case?
Hope this helped.

How to set the Default Value of a ListPreference

i need to set the defult value for a ListPreference when the Activity starts.
I've tried with ListPreference.setDefaultvalue("value"); but it makes the firts Entry of the List as default. I need it because i must check a condition and set as default the value which satisfies that condition, so I think it can't be done from the xml file (with android:defaultValue)
For example, suppose I have this array of values in the arrays.xml:
<string-array name="opts">
<item>red</item>
<item>green</item>
<item>blue</item>
</string-array>
<string-array name="opts_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
In the PreferenceScreen xml:
<ListPreference
android:title="Colour select"
android:summary="Select your favourite"
android:key="colour"
android:entries="#array/opts"
android:entryValues="#array/opts_values" />
In the Activity I'd like to do something like this:
String mycolour;
if (something) {
mycolour="1";
} else {
mycolour="2";
}
ListPreference colour = (ListPreference) findPreference ("colour");
colour.setDefaultValue(mycolour);
But it doesn't work, because it makes the first choice as default. Could you explain me how to make another one as default? Thanks.
You don't need to programmatically handle the default value of ListPreferences. You can do this in xml setting file. Below is an example
<string-array name="opts">
<item>red</item>
<item>green</item>
<item>blue</item>
</string-array>
<string-array name="opts_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
...
<ListPreference
android:title="Colour select"
android:summary="Select your favourite"
android:key="colour"
android:entries="#array/opts"
android:entryValues="#array/opts_values"
android:defaultValue="2" />
here I selected 2 as a default value. Remember defaultvalue will be opts_values element.
Have you tried:
setValueIndex(int index);
Sorry my bad English.
List item
Retrieve the list Check if the value is null. If it is null set to the default value.
Code:
ListPreference dataPref = (ListPreference) findPreference("keyList");
if(dataPref.getValue() == null){
dataPref.setValueIndex(0); //set to index of your deafult value
}
You can set your default value by using the key like this
<string-array name="syncFrequency">
<item name="1">Block All Calls</item>
<item name="2">Block Black List</item>
<item name="3">Block Unknown Calls</item>
<item name="4">Allow White List</item>
<item name="5">Receive All Calls</item>
</string-array>
<string-array name="syncFrequencyValues">
<item name="1">Block_All_Calls</item>
<item name="2">Block_Black_List</item>
<item name="3">Block_Unknown_Calls</item>
<item name="4">Allow_White_List</item>
<item name="5">Receive_All_Calls</item>
</string-array>
<ListPreference
android:key="prefSyncFrequency"
android:entries="#array/syncFrequency"
android:summary="%s"
android:defaultValue="Block_Black_List"
android:entryValues="#array/syncFrequencyValues"
android:title="#string/call_block_options" />
or you can also try colour.setValue(mycolour);
Just for the record if someone else has this problem:
setValueIndex(int X) is setting the value # index X to the default value - so it is probably what you are looking for.
Set this value AFTER you added the Values! (stupid mistake but took me half an hour)
Actually it's because the SharedPreferences will persist after you re-build the app.
Uninstall it and try again.
((ListPreference) findPreference("pref_language")).setValue(Locale
.getDefault().getLanguage());
setValue() is ListPreference's method, and setDefaultvalue is Preference's method
This is an old post, but here's another way to set the default value for ListPreference with the following line of code:
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
Use the xml attribute android:defaultValue="<VALUE>" in your list tag to set the default value.
Note: the <VALUE> is the actual value and not the index of string
array.
If still its not working, try below steps.
Clear application data.
Uninstall and reinstall the app
Check the list preference, you will see the default value selected
Strange, I know but it worked in my case.
If you are using Android Jetpack Preference, known as androidx.preference:preference-ktx:1.1.1 in Kotlin, you can use:
app:defaultValue="<Value_in_string-array_with_values>".
Additionally: defaultValue is not an index number, is the actual value from the values array.
I also recommend using a string resource for the default value and clearing the data, uninstalling the app, or removing the file:
<package_name_in_app_manifest>_preferences.xml in data/data/shared_prefs/<package_name_in_app_manifest>. Replace <package_name_in_app_manifest> with real name like com.example.yourapp.
I was having the same issue and the defaultValue wasn't updating because it already had a wrong default value of "true". Solved it by using the Android Studio File Explorer and deleting the file.
And here is my solution example:
res/xml/root_preferences.xml
<ListPreference
app:key="mic"
app:title="#string/select_mic_title"
app:useSimpleSummaryProvider="true"
app:entries="#array/mic_entries"
app:entryValues="#array/mic_values"
app:defaultValue="#string/default_mic"
 />
res/values/arrays
<!-- Mic Preference -->
<string-array name="mic_entries">
<item>#string/default_mic</item>
<item>Headband</item>
</string-array>

<string-array name="mic_values">

 <item>#string/default_mic</item>
<item>Headband</item>

</string-array>
res/strings.xml
<string name="default_mic">Integrated</string>
Result:
Setting Fragment with default list value of "Integrated" given by string resource #string/default_mic

Android: Context menu doesn't show up for ListView with members defined by LinearLayout?

I've got a ListActivity and ListView and I've bound some data to it. The data shows up fine, and I've also registered a context menu for the view. When I display the list items as just a simple TextView, it works fine:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
However when I try something a bit more complex, like show the name and a CheckBox, the menu never shows up:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="#+id/namecheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Can long-presses work on more complex elements? I'm building on 2.1.
(edit)
Registering with this on the ListActivity:
registerForContextMenu(getListView());
The code I posted is the item template for the list.
Your CheckBox may be interfering with matters. Consider using a CheckedTextView instead of a LinearLayout, CheckBox, and TextView combination, since CheckedTextView is what Android expects for a CHOICE_MODE_MULTIPLE list.
Check out $ANDROID_HOME/platforms/$VERSION/data/res/layout/simple_list_item_multiple_choice.xml, where $ANDROID_HOME is wherever you installed the SDK and $VERSION is some Android version (e.g., android-2.1). This resource is the standard resource you should use for CHOICE_MODE_MULTIPLE lists. Feel free to copy it into your project and adjust the styling of the CheckedTextView as needed.
set checkbox property
focusable = false;
and run project again..
Found at this place: http://www.anddev.org/view-layout-resource-problems-f27/custom-list-view-row-item-and-context-menu-t52431.html
Setting the checkbox to not be focusable fixes the problem.
Not sure if it would cause issues when navigating the UI with something else than a touchscreen (with a wheel or arrow keys), but it fixed my problem (my layout was a bit more complicated than just a TextView and a Checkbox...)
Context menu's can only be registered to subclasses of View. I don't know how you registered the LinearLayout with a context menu, did you package it in some type of View? if so, you should post that code.
Anyways why not just register the TextView of each list item? Who would long press a checkbox...
This should from a regular ListView as well. But if you're starting from scratch on a new list I would consider using the CheckedTextView:
checkBox.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// return false to let list's context menu show
return false;
}
});

Categories