I have to recognize some alternative rules, but I don't know how to do a mutual exclusion.
For example, if I want to recognize "play", "stop", or "set 1", "set 2", how can I do? I tried something like this, but it doesn't recognize when I don't need the number (for example, "start 2" is correctly recognized, but trivially I don't want to).
<grammar version="1.0" tag-format="semantics/1.0" xml:lang="en-US" root="main">
<rule id="main">
<ruleref uri="#actions"/>
<ruleref uri="#numbers"/>
</rule>
<rule id="actions">
<item repeat="0-1">
<one-of>
<item>play</item>
<item>stop</item>
<item>set</item>
</one-of>
</item>
</rule>
<rule id="numbers">
<item repeat="0-1">
<one-of>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
</one-of>
</item>
</rule>
</grammar>
Simply I found out that if I put
<item>play <ruleref special="GARBAGE"/></item>
<item>stop <ruleref special="GARBAGE"/></item>
<item>set <ruleref special="GARBAGE"/><ruleref uri="#numbers"/></item>
calling the id reference after set will take just "set 1", "set 2", but doesn't accept "start 1" because there isn't the reference after the name.
Related
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
I have a custom intent for app actions to say something like these sentences:
Hey Google, open MyCookingApp to bake a Cake for 10 minutes
Hey Google, open MyCookingApp to bake a Pizza for 15 minutes
Hey Google, open MyCookingApp to bake a Pizza (here the user doesn't specify any time)
To do this I use this custom intent:
<action intentName="custom.actions.intent.BAKE_SOMETHING" queryPatterns="#array/ExampleQueries">
<!-- Define parameters -->
<parameter name="text1" type="https://schema.org/Text" />
<parameter name="number1" type="https://schema.org/Number" />
<!-- Define fulfillment -->
<fulfillment urlTemplate="https://my.app.com/{?item_name,number_of_minutes}">
<parameter-mapping intentParameter="text1" urlParameter="item_name" />
<parameter-mapping intentParameter="number1" urlParameter="number_of_minutes" />
</fulfillment>
</action>
And this is the ExampleQueries:
<string-array name="ExampleQueries">
<item>bake a $text1</item>
<item>bake a $text1 for $number1 minutes</item>
</string-array>
If I just say "open MyCookingApp to bake a Pizza" then I get item_name="Pizza" and number_of_minutes=null, which is correct!!
But if I say "open MyCookingApp to bake a Pizza for 15 minutes" then I get item_name="Pizza for 15 minutes" and number_of_minutes=15, which is WRONG!! Why do I get "Pizza for 15 minutes" as the the first parameter?
I have tried to have 2 different custom intents. One for only the item "bake a $text1" and another one for the item "bake a $text1 for $number1 minutes" but I get same results. It seems like the Assistant takes the "easiest" way. I mean, if "bake a $text1" is provided, then everything after "bake a" will be considered a string. But in this case why do I get a real value for number_of_minutes? It should be null, right?
Any body can show some light here. Basically I need to ask the users for a first string value, and then an optional number.
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!!
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.
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.