Android no set default values - android

I followed all the instructions for setting default preference values step by step but does not work.
1º I set the default values:
<PreferenceCategory android:title="#string/settings_game" >
<ListPreference
android:defaultValue="1.5"
android:entries="#array/intervale_count"
android:entryValues="#array/intervale_count_values"
android:key="interval"
android:title="#string/interval" />
<ListPreference
android:defaultValue="#string/mode_normal"
android:entries="#array/mode_game"
android:entryValues="#array/mode_game_values"
android:key="mode"
android:title="#string/mode" />
</PreferenceCategory>
<PreferenceCategory android:title="#string/settings_sound" >
<ListPreference
android:defaultValue="#string/stone"
android:entries="#array/time_sounds"
android:entryValues="#array/time_sounds_values"
android:key="time_sounds"
android:title="#string/sounds_stones" />
<ListPreference
android:defaultValue="#string/vuvucela"
android:entries="#array/gong_sounds"
android:entryValues="#array/gong_sounds_values"
android:key="gong_sounds"
android:title="#string/sounds_gong" />
</PreferenceCategory>
2º I get the preferences in my MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
}
But when I open the preference view no preferences are selected, the strings of I declare in the android:defaultValue is the SAME as one of the options Ihave in the array of data in android:entries.
Note: I try to change to true the boolean in
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
but that still does not work.
I've also tried to uninstall and delete the data of the app but that still does not work.
Edit: the array values
<string-array name="intervale_count">
<item>1</item>
<item>1.3</item>
<item>1.5</item>
<item>1.7</item>
<item>2</item>
</string-array>
<string-array name="intervale_count_values">
<item>1000</item>
<item>1300</item>
<item>1500</item>
<item>1700</item>
<item>2000</item>
</string-array>
<string-array name="mode_game">
<item>#string/mode_normal</item>
<item>#string/mode_extension</item>
<item>#string/mode_infinite</item>
</string-array>
<string-array name="mode_game_values">
<item>100</item>
<item>50</item>
<item>1000</item>
</string-array>
<string-array name="time_sounds">
<item>#string/alan</item>
<item>#string/cash_reg</item>
<item>#string/censure</item>
<item>#string/crow</item>
<item>#string/doh</item>
<item>#string/fb_noti</item>
<item>#string/achievement</item>
<item>#string/metal_gear</item>
<item>#string/duck</item>
<item>#string/pan</item>
<item>#string/drum</item>
<item>#string/stone</item>
</string-array>
<string-array name="time_sounds_values">
<item>2131099648</item> <!-- Alan -->
<item>2131099649</item> <!-- Registradora -->
<item>2131099650</item> <!-- Censura -->
<item>2131099651</item> <!-- Cuervo -->
<item>2131099652</item> <!-- DOh -->
<item>2131099653</item> <!-- FB -->
<item>2131099655</item> <!-- logro -->
<item>2131099656</item> <!--metal gear -->
<item>2131099657</item> <!-- pato -->
<item>2131099658</item><!-- sarten -->
<item>2131099660</item> <!-- tambor -->
<item>2131099659</item><!-- stone -->
</string-array>
<string-array name="gong_sounds">
<item>#string/gong</item>
<item>#string/vuvucela</item>
</string-array>
<string-array name="gong_sounds_values">
<item>2131099654</item> <!-- Gong -->
<item>2131099661</item> <!-- Vuvucela -->
</string-array>

The defaultValue attribute for a ListPreference needs to be a value, not the entry text. In other words, it should be an element in the array you pass to android:entryValues, not an element in the array you pass to android:entries.
For example, your gong sounds preference should use android:defaultValue="2131099661" instead of android:defaultValue="#string/vuvucela"

If it doesn't work, try to wipe the app data from the emulator/device and then reinstall the app. After I did that in my project and followed Tanis hints, it worked like a charm.

Related

Android: Drawer items doesnt change title when language is changed

I implemented some string resources for english and romanian. Switching the locale works fine for text fields, but the titles for the drawer items doesnt change when language is changed.
I have the following configuration:
Setting the locale to "ro" in MainActivity
private fun setDefaultLocale(lang: String) {
val config = android.content.res.Configuration(resources.configuration)
val locale = Locale(lang)
Locale.setDefault(locale)
config.setLocale(locale)
config.setLayoutDirection(locale)
resources.updateConfiguration(config, resources.displayMetrics)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setDefaultLocale("ro")
.....
}
Menu resources:
<group android:checkableBehavior="single">
<item
android:icon="#drawable/ic_baseline_home_24"
android:id="#+id/home"
android:title="#string/menu_home" />
<item
android:icon="#drawable/ic_baseline_wallpaper_24"
android:id="#+id/news"
android:title="#string/menu_news" />
<item
android:icon="#drawable/ic_jungle"
android:id="#+id/jungleTimer"
android:title="#string/menu_timer" />
<item
android:icon="#drawable/ic_damage"
android:id="#+id/builds"
android:title="#string/menu_builds" />
<item
android:icon="#drawable/ic_baseline_settings_24"
android:id="#+id/settings"
android:title="#string/menu_settings" />
<item
android:icon="#drawable/ic_baseline_info_24"
android:id="#+id/about"
android:title="#string/menu_about" />
<item
android:icon="#drawable/ic_feedback"
android:id="#+id/feedback"
android:title="#string/menu_feedback" />
<item
android:icon="#drawable/ic_favorite"
android:id="#+id/rateapp"
android:title="#string/menu_rate" />
</group>
Default string xml file:
/* Menu translations */
<string name="menu_home">Home</string>
<string name="menu_news">News</string>
<string name="menu_timer">Jungle Timers</string>
<string name="menu_builds">Guides</string>
<string name="menu_settings">Settings</string>
<string name="menu_about">About</string>
<string name="menu_feedback">Feedback</string>
<string name="menu_rate">Rate the app</string>
Romanian strings xml file
/* Menu translations */
<string name="menu_home">Acasă</string>
<string name="menu_news">Noutăți</string>
<string name="menu_timer">Cronometru Junglă</string>
<string name="menu_builds">Ghid Campioni</string>
<string name="menu_settings">Setări</string>
<string name="menu_about">Despre</string>
<string name="menu_feedback">Feedback</string>
<string name="menu_rate">Dă-ne o notă</string>
Updating configuration will not restart your activity, so Drawer will not know that you changed locale on the device thus not changing the texts.
If you change your language in system settings it should work.
I propose recreating the activity after you change your configuration programmatically, for example with activity?.recreate() method

Cannot Locate Strings that Exist

I have the following error:
From menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_item_new"
android:title="#string/menu_item_new"
appcompat:showAsAction="always"></item>
<item
android:id="#+id/menu_item_save"
android:title="#string/menu_item_save"
appcompat:showAsAction="always"></item>
<item
android:id="#+id/menu_item_delete"
android:title="#string/menu_item_delete"
appcompat:showAsAction="always"></item>
<item
android:id="#+id/menu_item_rate"
android:title="#string/menu_item_rate"></item>
<item
android:id="#+id/menu_item_website"
android:title="#string/menu_item_website"></item>
<item
android:id="#+id/menu_item_report"
android:title="#string/menu_item_report"></item>
</menu>
Even though all those strings do in fact exist:
<string name="menu_item_new">New</string>
<string name="menu_item_save">Save</string>
<string name="menu_item_delete">Cancel</string>
<string name="menu_item_rate">Rate on Google Play</string>
<string name="menu_item_website">Website</string>
<string name="menu_item_report">Report a Bug</string>
I have attempted to simply re-write the code as well as clean and rebuild, yet the error persists.
How can this be resolved?
resources tag is missing from strings.xml file.
Android couldn't locate the string resource.
Make following changes:
<resources>
<string name="menu_item_new">New</string>
<string name="menu_item_save">Save</string>
<string name="menu_item_delete">Cancel</string>
<string name="menu_item_rate">Rate on Google Play</string>
<string name="menu_item_website">Website</string>
<string name="menu_item_report">Report a Bug</string>
</resources>

How to increase brightness of screen on Android?

I'm trying to develop an app which change the brightness in a PreferenceActivity but when i choose the item, the app is closed... why??
The Code
<ListPreference
android:key="brillo"
android:title="Luminosidad"
android:summary="Escoge cuanto brillo quieres para jugar"
android:dialogTitle="Selecciona tono"
android:entries="#array/modobrillo"
android:entryValues="#array/codigobrillo"
/>
the arrays
<string-array name="modobrillo">
<item>Nivel Bajo</item>
<item>Nivel Medio</item>
<item>Nivel Alto</item>
<string-array name="codigobrillo">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
On a Activity I do not even have code... Help!!

What does "add-resource" in Android resource xml file do?

I found that there are following statement in package/apps/UnifiedEmail package in android source code:
<add-resource name="RecipientEditTextViewStyle" type="style" />
What does this "add-resource" element mean?
The source code: https://github.com/CyanogenMod/android_packages_apps_UnifiedEmail/blob/cm-11.0/res/values/attrs.xml
<resources>
<string name="app1_name">MyAppName</string>
</resources>
is the same thing as
<resources>
<add-resource type="string" name="app1_name">MyAppName</add-resource>
</resources>
so
<add-resource name="RecipientEditTextViewStyle" type="style" />
is the same thing as
<style name = "RecipientEditTextViewStyle"/>

Incorrect values when I select a radioButton in ListPreference

I have a Preference activity which uses a List Preferences as defined by my XML file:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="sync">
<ListPreference
android:key="key_sync_period"
android:title="Sync"
android:summary="%s"
android:dialogTitle="Sync frequency"
android:entries="#array/sync_period_entries"
android:entryValues="#array/sync_period_values"
android:defaultValue="1800" />
</PreferenceCategory>
</PreferenceScreen>
arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sync_period_entries">
<item>15 min</item>
<item>30 min</item>
<item>45 min</item>
<item>1 hour</item>
</string-array>
<string-array name="sync_period_values">
<item>900</item>
<item>1800</item>
<item>2700</item>
<item>3600</item>
</string-array>
</resources>
When I start my app, change this setting, I see the following:
the app writes to a defaultSharedPreference file value = 1800
I change it to "15 min" and see value = 900 - ok, all right
I change it to "45 min" and see value = 60 - ???
I change it to "1 hour" and see value = 180 - ???????
I change again it to "30 min" and see value = 1800
I change it to "1 hour" and see value = 180 - why?
Why do I see these values (60, 180)? Where did they come from?
Update 1:
if (key.equals("key_sync_period"))
{
ListPreference syncPref = (ListPreference) findPreference(key);
syncPref.setSummary(syncPref.getEntry());
long seconds = Long.valueOf(sharedPreferences.getString(key, "1800"));
}
The system writes defaultSharedPreferences. I see preference file from ddms and see that the system written wrong value.
The problem was in the Idea 12CE, I deleted the project and imported it again... and problem was solved.

Categories

Resources