I am trying to set up the default value for the list preferences in my app, but I am having trouble with the default value, as when I try to run the app the default values that I chose in both list preferences are not being selected in runtime. This is my xml code:
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory app:title="#string/display_header">
<ListPreference
app:key="language"
android:title="#string/language_title"
app:summary="#string/en"
android:entries="#array/lang_entries"
android:entryValues="#array/lang_values"
android:defaultValue="#string/en"
/>
<ListPreference
app:key="display"
app:title="#string/display_mode_title"
app:entries="#array/display_entries"
app:entryValues="#array/display_values"
app:summary="#string/automatic"
android:defaultValue="#string/automatic"/>
</PreferenceCategory>
</PreferenceScreen>
I tried uninstalling and reinstalling the app but it did not work. I also checked that the string names that I used in the default value are correct, and I also made sure that the default value matches the one in the string array of entries. Please help me fix this problem
I think defaultValue is a number/index of the choice.
See this post.
I just found out that I had to replace the android:defaultValue line by app:defaultValue and I had to pass the corresponding value to the entry that would be the default value in runtime.
Related
I can't seem to understand why I keep getting the "default" string show up when I grab input from the EditTextPreference.
<EditTextPreference
android:title="#string/settings_title_signature"
android:summary="#string/settings_enter_signature"
android:singleLine="true"
android:key="edit_signature_key"
/>
I never seem to get what the textfield has using Sharedpreferences. It just defaults to the "default", and not what should be in the key.
SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this);
String sig = myPreference.getString("edit_signature_key", "default");
Make sure you are using the EditTextPreference inside of a Preference Screen or Preference Fragment. Sounds like you are just using a standalone EditTextPreference.
If you are using it in the right container, please update your post with all of your code and I will be able to help.
(had to put this as an answer instead of a comment because I don't have enough rep)
I'm trying to extend the DialogPreference so I can have a NumberPicker for one of my
preferences. I've been reading:
http://developer.android.com/guide/topics/ui/settings.html#Custom
But I'm hitting an 'Error inflating NumberPickerReference' message. I think I know
why, but I'm not sure how to fix it.
My preferences.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="pref_alertsEnabled"
android:title="#string/alert_enable"
android:summary="#string/alert_enable_desc"
android:defaultValue="true" />
<NumberPickerPreference
android:key="perf_Timer"
android:title="#string/timer"
android:negativeButtonText="#android:string/cancel"
android:dialogTitle="#string/timer"
android:summary="#string/timer_desc"
android:dialogMessage="#string/timer_desc"
android:positiveButtonText="#android:string/ok"/>
</PreferenceScreen>
And in my projects src folder I have NumberPickerReference.java. I think the thing I'm missing is how to tell Android where to find NumberPickerReference class.
When preferences.xml is read in, I'm guessing it goes looking for the NumberPickerReference class, but doesn't find it.
Do I need to specify where to find the NumberPickerReference.class ? If yes, how do I do this ?
BTW, the app runs fine if I remove the <NumberPickerReferecnes> item from preferences.xml
Just add the path to that preference:
com.your.package_name.and_path.NumberPickerPreference
E.g.
<com.example.myapp.preferences.NumberPickerPreference
android:key="perf_Timer"
android:title="#string/timer"
android:negativeButtonText="#android:string/cancel"
android:dialogTitle="#string/timer"
android:summary="#string/timer_desc"
android:dialogMessage="#string/timer_desc"
android:positiveButtonText="#android:string/ok"/>
I have never really had to ask a question here. Almost any question I can think of has already been asked and answered. Thus the reason I have never registered.
However, I have finally run across something that I cannot find an answer for. I assume it would be fairly simple.
I am trying to implement in app billing for an android project I am working on. I typically code in C# and use Unity to build the .APK.
However, this time I have to make modifications in Eclipse to some PreferenceScreens.
I have an xml file with some string-arrays set up for a ListPreference under my PreferenceScreen. These ListPreference values are then passed to UnityPlayer for a method. That's all set up.
I am trying to figure out a way to set up in app billing to trigger on selection of one of my list preference, or preference items.
I can probably figure a lot of it out on my own, but I need help with a function to do something pulling from a string value in the list preferences. Does this make sense?
I don't expect to be spoon fed, so if I can just get help on a few lines for this I would appreciate it. Of course I won't complain if anyone wants to write up a little 20 liner function to do it for me.
Thanks in advance,
John
** edit **
Here is the xml from the preferenceScreen I need to address with android In App Billing from Java
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="wallpaper_settings"
android:title="#string/wallpaper_settings" >
<!-- Ad Placeholder -->
<CheckBoxPreference
android:defaultValue="true"
android:key="rotate"
android:summary="#string/rotateSummary"
android:title="#string/rotateTitle" />
<CheckBoxPreference
android:defaultValue="true"
android:key="doubleTap"
android:summary="#string/doubleTapSummary"
android:title="#string/doubleTapTitle" />
<CheckBoxPreference
android:defaultValue="false"
android:key="swipeEmul"
android:summary="#string/simulateSwipeSummary"
android:title="#string/simulateSwipeTitle" />
<ListPreference
android:entryValues="#array/cameraValues"
android:defaultValue="MainCamera"
android:entries="#array/whichCam"
android:summary="#string/cameraSummary"
android:dialogTitle="#string/cameraTitles"
android:title="#string/cameraTitles"
android:key="whichCam"/>
</PreferenceScreen>'
This question is very generic, on an Android application you might do this by creating an Activity, then starting that activity from your preference "onClick" method and then, once the activity starts, you load all your in app billing stuff (showing a progress dialog) and finally present the "buy" dialog. Is this what you need?
I am following developer.android to do their training. I am in the process of "Adding the Action Bar", however, i encountered a problem.
In the section "Respond to Action Buttons" here (http://developer.android.com/training/basics/actionbar/adding-buttons.html#Respond), the line "case R.id.action_search:" has error which says "action_search cannot be resolved or is not a field".
[Delete] In fact, the line "openSearch();" and "openSettings();" have red lines as well, saying "The method openSettings() is undefined for the type MainActivity". [/Delete]
(I know what is wrong here, I need to declare those two methods by myself.)
what should I do? Thx for the help first.
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
Firstly, the attribute android:id="#+id/action_search has no problem.
There are two things you need to do that the tutorial seems to have taken for granted
Add an image called "ic_action_search.png" to all the res/drawable directories in your project.
Add the definition for the string resource "action_search" which is being assigned to the android:title attribute. Open the res/values/strings.xml file and add
<string name="action_search">Search</string> over there.
Secondly,
The functions openSearch() and openSettings() need to be defined in the same class by you. Something like :
public void openSearch()
{
//Do something here.
}
public void openSettings()
{
//Do something here.
}
their functionality is not important for the tutorial as they are trying to show how you can control what happens when an option on the action menu is selected.
Go Project->Clean
It worked for me
I tried it and had the same problem. I removed android:icon="#drawable/ic_action_search" (instead of adding the images because I'm lazy) and added the action_search string in the resource file to make sure the menu file wasn't referring to anything that didn't exist. After that, I tried changing android:id to android:title because I saw that it had worked for you, and this was curious. It caused an error after saving, and so I changed it back. After changing it back and saving, the original error in the MainActivity.java file disappeared, and R.id.action_search was resolved. I think the problem is just that R.java is not being updated, and you have to basically hit it with a hammer by forcing an error and fixing it, at least in my case. I don't know why using android:title worked for you. Hope this helps somehow.
I am trying to setup activity screen orientation with values from XML file in res/values. I would like to do that because, more or less, I need same Activity for both tablet (landscape) and smartphone (portrait).
First try
Manifest:
<activity android:name="..." android:screenOrientation="#string/defaultOrientation"/>
config.xml:
<string name="defaultOrientation">portrait</string>
But with this setting application won't appear on device and it will return this error:
java.lang.NumberFormatException: Invalid int: "portrait"
Second
OK, so I simply changed it to this
Manifest:
<activity android:name="..." android:screenOrientation="#integer/defaultOrientation"/>
config.xml:
<integer name="defaultOrientation">1</integer>
I used 1 because ActivityInfo.SCREEN_ORIENTATION_PORTRAIT == 1.
But this is not working either. It seems that I may modify some values like application / activity name but not screen orientation ?
I know that I may workaround it by code but for some reason it feels that this also should be obtainable by XML values file.
It is somehow possible to achieve it by XML values ?
Same problem for me with your second exlanation and I used a workaround by code which you aren't looking for.
I added 4 values folders under res folder. "values", "values-v11", "values-v14" and "values-sw720dp"
All values folders have "integers.xml".
"values" and "values-v14" have value 1 which is portrait orientation;
<integer name="portrait_if_not_tablet">1</integer>.
"values-v11" and "values-sw720dp" have value 2 which is user orientation;
<integer name="portrait_if_not_tablet">2</integer>.
And in Manifest file, activity has a property like;
android:screenOrientation="#integer/portrait_if_not_tablet".
All "values", "values-v11", "values-v14" are working as expected but "values-sw720dp"!
While debugging I realized that value of portrait_if_not_tablet comes as expected on a sw720dp device(with API 16) with getResources().getInteger(R.integer.portrait_if_not_tablet) but when i checked the value of current orientation by getRequestedOrientation() I got a different value.
int requestedOrientation = getResources().getInteger(R.integer.portrait_if_not_tablet);
int currentOrientation = getRequestedOrientation();
if (currentOrientation != requestedOrientation) {
setRequestedOrientation(requestedOrientation);
}
So I used a code block on onCreate method of my activities to solve this.