PreferenceActivity MultiSelectListPreference uncheck all - android

When first time I'm opening MultiSelectListPreference or when uncheck all check boxes and reopen MultiSelectListPreference, all items are displaying as checked.
I need to uncheck all check boxes on above situations.
How can I solve this?
settings.xml
<MultiSelectListPreference
android:entries="#array/pref_color_list_titles"
android:entryValues="#array/pref_color_list_values"
android:key="prefColor"
android:summary="#string/pref_color_summary"
android:title="#string/pref_color_title" >
</MultiSelectListPreference>
strings.xml
<string-array name="pref_color_list_titles">
<item>Red</item>
<item>Green</item>
<item>Blue</item>
</string-array>
<string-array name="pref_color_list_values">
<item>r</item>
<item>g</item>
<item>b</item>
</string-array>
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}

You need just to pass empty Set to MultiSelectListPreference
Set<String> uncheckValues = new HashSet<>();
myMultiSelectListPreference.setValues(uncheckValues);
If you want check all values, you can do this one
String[] defaultValuesAsArray = getResources().getStringArray(R.array.default_values_multiselectlist_pref);
Set<String> defaultValues = new HashSet<>(Arrays.asList(defaultValuesAsArray)); //convert array to set
myMultiSelectListPreference.setValues(defaultValues);

Related

Selecting a specific string array

I have a question and i cant find a solution myself. I am using for example the following string arrays:
<string-array name="alphabet">
<item>ccc</item>
<item>bbb</item>
<item>aaa</item>
</string-array>
<string-array name="ccc">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="bbb">
<item>4</item>
<item>5</item>
<item>6</item>
</string-array>
<string-array name="aaa">
<item>7</item>
<item>8</item>
<item>9</item>
</string-array>
So, The first string array alphabet is placed inside a spinner. (dropdown menu) Lets say i select CCC, Then i want only the items in between the array CCC to be worked with and stored in an array These information in this array should then be randomized and formatted into groups later on. But i only need a selection of the string selected.
So is there a way to select just one array based on the choice made out of the first array?
Kind regards,
Here is the code, the way you needed.
Step 1] Set, setOnItemSelectedListener on first spinner and get the Arrayname for target spinner
spinner_alphabates.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
String arrayName = spinner_alphabates.getSelectedItem().toString();
int resId = getResources().getIdentifier(arrayName,"array",getPackageName());
setResultArray(resId);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Step 2] set the result array on target spinner as
public void setResultArray(int resID)
{
String [] result_array = getResources().getStringArray(resID);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, result_array);
spinner_result.setAdapter(adapter);
}
No need to put 'if' 'else' conditions to match array names.
Hope this help you.
Try this, I am not sure whether it is going to work, just have try
<string-array name="ccc">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<integer-array name="id">
<item> #array/ccc </item>
</integer-array>
You need to get array of string like this. and you will access it with index of string array.
String.xml
<string-array name="my_books">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
</string-array>
Get List of string in Java file like :
Resources res = getResources();
String[] myBooks = res.getStringArray(R.array.my_books);
Try this method and put this method into onCreate in activity method..
change this array add one more item..
<string-array name="alphabet">
<item>Select</item>
<item>ccc</item>
<item>bbb</item>
<item>aaa</item>
</string-array>
private void initView() {
spinner = findViewById(R.id.spinner);
final List<String> asList = Arrays.asList(getResources().getStringArray(R.array.alphabet));
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, asList);
final ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (spinner.getSelectedItem().equals("ccc")) {
adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.ccc)));
spinner.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
} else if (spinner.getSelectedItem().equals("aaa")) {
adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.aaa)));
spinner.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
} else if (spinner.getSelectedItem().equals("bbb")) {
adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.bbb)));
spinner.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}

Programmatically Creating ListPreference - But Entry List is Empty

I'm trying to programmatically create a ListPreference, which I can do but when I select it, it's list of entries is empty. I believe I am correctly setting the setEntries() and setEntryValues() with CharSequence arrays, but it's just empty when I select it.
Please find below the ActivitySetting class. Please note I'm using PreferenceFragments as to not use deprecated methods. But I only have one PreferenceFragment which is currently set as default
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();
}
public static class PrefsFragment extends PreferenceFragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
// Create the new ListPref
ListPreference customListPref = new ListPreference(getActivity());
// Get the Preference Category which we want to add the ListPreference to
PreferenceCategory targetCategory = (PreferenceCategory) findPreference("TARGET_CATEGORY");
CharSequence[] entries = new CharSequence[]{"One", "Two", "Three"};
CharSequence[] entryValues = new CharSequence[]{ "1", "2", "3" };
// IMPORTANT - This is where set entries...looks OK to me
customListPref.setEntries(entries);
customListPref.setEntryValues(entryValues);
customListPref.setTitle("Title");
customListPref.setSummary("This is the summary");
customListPref.setDialogMessage("Dialog Message");
customListPref.setPersistent(true);
// Add the ListPref to the Pref category
targetCategory.addPreference(customListPref);
}
}
}
Here is the Setting.xml it just has the single PreferenceCategory which the ListPreference is added to:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Some Options" android:key="TARGET_CATEGORY">
</PreferenceCategory>
</PreferenceScreen>
Here is what I get. The ListPreference has been successfully but when I select it.... No entries :( I'm expecting the options: "One", "Two", "Three"
Found it if you set the setDialogMessage() then this overwrites the contents so by removing this line, it works now.
You might want to replace setDialogMessage() by setDialogTitle() to get the title back.

Spinner Android adapter in main Activity

Trying to simply get a Spinner going in my Application but the lines (commented out, the app works fine without these 2 lines) give me errors every time I try to start the Activity. I setup an Array in Strings.XML to be used in conjunction with the Spinner to view the data.
My XML contains a Spinner like so:
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
In Strings.XML I have my Array:
<string-array name="spinner_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
My Main Activity, 2 lines that are commented cause error.
public class BusPurchase extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_array, android.R.layout.simple_spinner_item);
super.onCreate(savedInstanceState);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//spinner.setAdapter(adapter);
setContentView(R.layout.activity_bus_purchase);
}
Log Cat Displays this:
http://chopapp.com/#cbz5r823
call setContentView before accessing views from xml layout as:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bus_purchase); // set layout here
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//..your code here
}
because you are trying to access views before setting layout for Activity
You are getting a Null Pointer on your references to spinner (R.id.spinner) because you are trying to reference items in your layout before you inflate the layout. Try calling setContentView(R.layout.activity_bus_purchase); first thing in your onCreate() method and see if that doesn't fix it.

How get selected option from ListPreference?

I have ListPreference and it contains for example 5 options and I want to save one of this value to SharedPreferences when user selects it. How can I do it?
btw. I know how to save value to SharedPreferences, but I don't know how to get that value when user selects one of them.
OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// newValue is the value you choose
return true;
}
};
listPreference.setOnPreferenceChangeListener(listener);
I access mine like this.. Please see example below
In my preference.xml file:
<ListPreference
android:key="SQS_ENDPOINT"
android:dialogTitle="Choose an option please"
android:entries="#array/sqsItems"
android:entryValues="#array/sqsValues"
android:title="SQS Endpoints" >
</ListPreference>
my String.xml:
<string-array name="sqsItems">
<item>US East (N. Virginia)</item>
<item>Asia Pacific (Singapore)</item>
<item>Asia Pacific (Tokyo)</item>
</string-array>
<string-array name="sqsValues">
<item>sqs.us-east-1.amazonaws.com</item>
<item>sqs.ap-southeast-1.amazonaws.com</item>
<item>sqs.ap-northeast-1.amazonaws.com</item>
</string-array>
And then I get the selected value like this from anywhere:
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(context);
String END_POINT = pref.getString("SQS_ENDPOINT", "");
In your xml file you provide SharedPreferences key for your list.
<ListPreference
android:key="SHARED_PREFS_KEY"
...
/>
Every time user selects item from the list it is saved to the default SharedPreferences

How to retrieve the preference from ListPreference?

I have ListPreference that has 4 choices/options, I want to check for the selected option and make some code(if 1 is selected I do that, if 2 is selected I do other thing ...).
XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="kernel">
<item>TalonDev</item>
<item>Semaphore</item>
<item>SpeedMod</item>
<item>Galaxian</item>
</string-array>
<string-array name="kernel_return">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>
Is that true :
choice = prefs.getString("listPref_kernel", "0");
if (choice == "0") {
try {
...................
If you are in a PreferenceActivity:
Retrieve your shared preferences:
SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
and retrieve the value:
String value = sp.getString(key, "default");
Optionally, you can set a SharedPreferences.OnSharedPreferenceChangeListener via
sp.registerOnSharedPreferenceChangeListener(...)
to be notified on any change.

Categories

Resources