I tried to convert int to curency format and everything worked as I expected
example 1000 --> Rp1.000
but on certain devices the results are not what I expected
i.e. 1000 --> Rp1.000,00
So the question is how to remove the last 2 extra nolls if the device displays them.
my current code:
public static String toUang(String DigitUang) {
return NumberFormat.getCurrencyInstance(new Locale("in", "ID")).format(Double.parseDouble(DigitUang));
}
The best solution here would probably be to fix your locale settings and handle the presentation there. That being said, you also could probably handle this using a regex replacement:
public static String toUang(String DigitUang) {
return NumberFormat.getCurrencyInstance(new Locale("in", "ID"))
.format(Double.parseDouble(DigitUang))
.replaceAll("[,.]00$", "");
}
I am using TimeZone.getDefault() to set the timezone of Calendar class:
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
Log.i("TEST", cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE));
However, when users change the timezone of their device from Setting, my application represents the time using the former timezone until they force stop (from App info settings) the application and restart it.
How could I prevent the caching of getDefault()?
It's not pretty, but you could potentially call setDefault(null) to explicitly wipe the cached value. As per the documentation, this will only affect the current process (that is, your app).
Having nulled out the cached value, the next time you call getDefault(), the value is reconstructed freshly:
/**
* Returns the user's preferred time zone. This may have been overridden for
* this process with {#link #setDefault}.
*
* <p>Since the user's time zone changes dynamically, avoid caching this
* value. Instead, use this method to look it up for each use.
*/
public static synchronized TimeZone getDefault() {
if (defaultTimeZone == null) {
TimezoneGetter tzGetter = TimezoneGetter.getInstance();
String zoneName = (tzGetter != null) ? tzGetter.getId() : null;
if (zoneName != null) {
zoneName = zoneName.trim();
}
if (zoneName == null || zoneName.isEmpty()) {
try {
// On the host, we can find the configured timezone here.
zoneName = IoUtils.readFileAsString("/etc/timezone");
} catch (IOException ex) {
// "vogar --mode device" can end up here.
// TODO: give libcore access to Android system properties and read "persist.sys.timezone".
zoneName = "GMT";
}
}
defaultTimeZone = TimeZone.getTimeZone(zoneName);
}
return (TimeZone) defaultTimeZone.clone();
}
You should probably combine this with a broadcast listener for ACTION_TIMEZONE_CHANGED and only null out the default value if you receive such a broadcast.
Edit: come to think of it, a neater solution would be to extract the newly set time zone from the broadcast. From the broadcast documentation:
time-zone - The java.util.TimeZone.getID() value identifying the new time zone.
You can then simply use this identifier to update the cached default:
String tzId = ...
TimeZone.setDefault(TimeZone.getTimeZone(tzId));
Any subsequent calls to getDefault() will then return the correct/updated time zone.
I'm using the speech recognizer to get a voice input from the user, it returns an array of 5 strings which I pass to this method
public int analyzeTag(ArrayList<String> voiceResults,Editor editor, Context context){
for (String match : voiceResults) {
Log.d(TAG, match);
if (match.equalsIgnoreCase(context.getResources().getString(R.string.first_tag))){
editor.append(context.getResources().getString(R.string.first_tag));
return 1;
}
else if (match.equalsIgnoreCase(context.getResources().getString(R.string.second_tag))){
editor.append(context.getResources().getString(R.string.second_tag));
return 1;
}
//etc....(huge list of tags)
//Some tags might also have acceptable variations, example:
else if (match.equalsIgnoreCase("img") || match.equalsIgnoreCase("image")
{
editor.append("img"); //the string to append is always taken from the first variation
}
}
return 0;
}
This method compares the results with a list of tags, the tag list will be pretty big with hundreds of tags so I would like to find the most efficient way to do this operation.
I need help with:
1.Is my way of comparing results the most efficient? Is there a better way? (from the user experience perspective, I don't want users waiting a long time to get a result).
The voice input will be a big part of my app so this method will be called quite often
2.I have a long list of tags, obviously the if(), elseIf() route is gonna be quite repetitive, is there a way to iterate this? Considering the fact that some tags might have variations (even more than 1)and that the variation 1 ("img") will be the same for everyone, but other variations will be locale/language sensitive example: "image" for english users "immagini" for italian users etc.
Text appended to the editor will be always taken from the first variation
How about puting tags in a StringArray and then iterate though the array ?
String[] tags = context.getResources().getStringArray(R.array.tags);
for (String match : voiceResults) {
for (int index = 0; index < tags.length; index++ ) {
if (match.equalsIgnoreCase(tags[index]) {
editor.append(tags[index]);
}
}
}
Here's the doc on StringArray
I've faced a problem with quantity strings (plurals).
The manual says, I may provide quantity strings that are specific for each localization, and there are several common cases: "zero", "one", "two", "few", "many", and "other". I don't know if all possible cases for all languages in the world were covered; anyway, it is more than enough for Russian that I'm trying to make a localization for.
In Russian, numbers from 2 to 4 should be treated like "few" (the rule is actually more complex but I only need numbers below ten).
However, when I request a quantity string for 2, the system takes the "other" string. It doesn't take neither "two" string nor "few" one (I have provided them in my resources). If I removed the "other" string, I get the exception:
android.content.res.Resources$NotFoundException:
Plural resource ID #0x7f080000 quantity=2 item=other
I tried this both on emulator (Android 2.1) and on a real device (Android 2.3), the behaviour is the same wrong in both cases. Obviously, there is a mistake somewhere—the system does not recognize locale-specific quantities for my language. Could it be that Android has some bug here?
I believe this is currently broken in Android.
http://code.google.com/p/android/issues/detail?id=8287
Specifically, the following code in PluralRules.java shows that most languages only use the one or other strings, but Czech will use the few string:
static final PluralRules ruleForLocale(Locale locale) {
String lang = locale.getLanguage();
if ("cs".equals(lang)) {
if (cs == null) cs = new cs();
return cs;
}
else {
if (en == null) en = new en();
return en;
}
}
private static PluralRules cs;
private static class cs extends PluralRules {
int quantityForNumber(int n) {
if (n == 1) {
return QUANTITY_ONE;
}
else if (n >= 2 && n <= 4) {
return QUANTITY_FEW;
}
else {
return QUANTITY_OTHER;
}
}
}
private static PluralRules en;
private static class en extends PluralRules {
int quantityForNumber(int n) {
if (n == 1) {
return QUANTITY_ONE;
}
else {
return QUANTITY_OTHER;
}
}
}
Thanks to Brigham who has pointed to the issue where the problem is explained; that explanation first rised more questions but now they seem to be solved. Indeed, the quantity strings don't work properly (at least prior to API 11, which is Android 3.x), so you have to use an alternative.
The solution for APIs prior to version 11 is mentioned in the comment 15 that contains a link to the project for alternative handling of quantity strings. That project is a program that simply does what the system was supposed to do. It can be converted to a library easily, so you just add it to your project, import the class and go.
Define your plurals strings in values-ru/strings.xml not in values/strings.xml
set your mobile device language as Russian (from setting->Language&input->Language->select Russian)
If your language is not Russian it will take plurals strings from values/strings.xml
Would anyone know where to control the ISO setting for the camera from in the Android SDK ?
It should be possible as the native camera application on the HTC Desire has ISO settings.
you should take a look at the methods flatten(), unflatten(), get(String key), set(String key, String value) in android.hardware.Camera.Parameters.
Also consider the source code of that class. It might make things clearer.
First you need to obtain the Camera.Parameters. Unflatten it to a String and investigate it. I am developing on a HTC Desire as well an get the following String:
sharpness-max=30;zoom=0;taking-picture-zoom=0;zoom-supported=true;sharpness-min=0;sharpness=10;contrast=5;whitebalance=auto;jpeg-quality=100;preview-format-values=yuv420sp;jpeg-thumbnail-quality=75;preview-format=yuv420sp;preview-size=640x480;focal-length=3.53;iso=auto;meter-mode=meter-center;front-camera-mode=mirror;flash-mode-values=off,auto,on,torch;preview-frame-rate-values=15;preview-frame-rate=15;focus-mode-values=auto,infinity;jpeg-thumbnail-width=640;jpeg-thumbnail-size-values=640x480,512x384,384x288,0x0;zoom-ratios=100,114,131,151,174,200;saturation-def=5;preview-size-values=1280x720,800x480,768x432,720x480,640x480,576x432,480x320,400x240,384x288,352x288,320x240,272x272,240x240,240x160,176x144,160x120;smart-contrast=off;picture-size-values=2592x1952,2592x1456,2592x1936,2592x1728,2592x1552,2048x1536,2048x1360,2048x1216,2048x1152,1600x1200,1584x1056,1280x960,1280x848,1280x768,1280x720,1024x768,640x480,640x416,640x384,640x368,512x384,400x400,272x272;contrast-min=0;min-exposure-compensation=-4;brightness-min=0;antibanding=auto;taking-picture-zoom-min=0;saturation-min=1;contrast-max=10;vertical-view-angle=42.5;taking-picture-zoom-max=21;contrast-def=5;brightness-max=6;horizontal-view-angle=54.8;brightness=3;jpeg-thumbnail-height=480;cam-mode=0;focus-mode=auto;sharpness-def=10;front-camera-mode-values=mirror,reverse;picture-format-values=jpeg;saturation-max=10;max-exposure-compensation=4;exposure-compensation=0;exposure-compensation-step=0.5;flash-mode=off;effect-values=none,mono,negative,solarize,sepia,posterize,aqua;meter-mode-values=meter-average,meter-center,meter-spot;picture-size=2592x1952;max-zoom=5;effect=none;saturation=5;whitebalance-values=auto,incandescent,fluorescent,daylight,cloudy-daylight;picture-format=jpeg;brightness-def=3;iso-values=auto,deblur,100,200,400,800,1250;enable-caf=off;antibanding-values=off,50hz,60hz,auto
So basically there is a key called iso-values to retrieve the supported values and a key iso which holds the current value.
You can do the following:
Camera cam = Camera.open();
Camera.Parameters camParams = cam.getParameters();
String supportedIsoValues = camParams.get("iso-values"); //supported values, comma separated String
camParams.set("iso", (String)newValue);
cam.setParameters(camParams);
And with reference to the unflattened parameters I would assume that there is a difference between the iso and exposure compensation settings.
By now (KK 4.4.2) android has no official APIs to manage ISO.
ISO management is a totally device dependant matter, and 8/18 devices i tested so far doesn't support ISO settings at all.
Investigate Camera.getParameters().flatten() String to check valid keywords, every device can use different keywords!!
Most devices use "iso-values" keyword to define a comma separated list-of-possible-values to use with "iso" keyword, like this:
param.set("iso", valid_value_from_list);
Some other devices uses "iso-mode-values" and "iso" keywords (Galaxy Nexus).
I found also a device that uses "iso-speed-values" and "iso-speed" (Micromax A101).
Another one that make me sad is "nv-picture-iso-values" -> "nv-picture-iso" (LG dual P990).
Follow szia answer on how to use these keywords.
Here's some code i use to get a list of valid values using known keywords:
String flat = param.flatten();
String[] isoValues = null;
String values_keyword=null;
String iso_keyword=null;
if(flat.contains("iso-values")) {
// most used keywords
values_keyword="iso-values";
iso_keyword="iso";
} else if(flat.contains("iso-mode-values")) {
// google galaxy nexus keywords
values_keyword="iso-mode-values";
iso_keyword="iso";
} else if(flat.contains("iso-speed-values")) {
// micromax a101 keywords
values_keyword="iso-speed-values";
iso_keyword="iso-speed";
} else if(flat.contains("nv-picture-iso-values")) {
// LG dual p990 keywords
values_keyword="nv-picture-iso-values";
iso_keyword="nv-picture-iso";
}
// add other eventual keywords here...
if(iso_keyword!=null) {
// flatten contains the iso key!!
String iso = flat.substring(flat.indexOf(values_keyword));
iso = iso.substring(iso.indexOf("=")+1);
if(iso.contains(";")) iso = iso.substring(0, iso.indexOf(";"));
isoValues = iso.split(",");
} else {
// iso not supported in a known way
}
since I had the same problem of finding if the device has an ISO parameter I looked at this answer https://stackoverflow.com/a/23567103/3976589 and saw that #j.c had solved the problem for 8/18 devices by listing some parameters that he had found on different devices. Based on that listing I found that each paramter contains the words iso and values (sometimes only those words, sometimes something additional).
So if I list all of the camera parameters and search for a strings that contain both words I will know what is the name of the ISO parameter, if it exists. Furthermore if the parameter exists I can take the supported ISO values and if I want to set one of those values i.e. change the camera parameter, I can just remove the -values at the end of the iso-values parameter and then I can change the ISO value successfully.
I will now share my code for this task. First a snippet that retrieves a list with supported ISO values.
private String ISOValuesParameter = null;
private String ISOParameter = null;
private String ISOValues = null;
private void initCamera() {
Camera mCamera = Camera.open();
// this will list supported values
String ISOvalues = getISOValues();
textViewISO = (TextView) findViewById(R.id.viewISO);
textViewISO.setText(ISOvalues);
// setting Minimum ISO value
if(ISOValuesParameter != null) {
Camera.Parameters params = mCamera.getParameters();
ISOParameter = ISOValuesParameter.replace("-values", "");
params.set(ISOParameter, getMinISO());
mCamera.setParameters(params);
// get the updated ISO value
params = mCamera.getParameters();
String ISO = params.get(ISOParameter);
Toast.makeText(this,"ISO set to: " + ISO, Toast.LENGTH_SHORT).show();
}
}
// returns a list with supported ISO values
private String getISOValues() {
ISOValuesParamter = getISOValuesParameter();
Camera.Parameters params = mCamera.getParameters();
ISOValues = params.get(ISOValuesParamter);
return ISOValues!=null ? ISOValues : "ISO not supported";
}
// this will return the name of the ISO parameter containing supported ISO values
private String getISOValuesParameter() {
Camera.Parameters params = mCamera.getParameters();
String flatten = params.flatten();
String[] paramsSeparated = flatten.split(";");
for(String cur : paramsSeparated) {
if(cur.contains("iso") && cur.contains("values")) {
return cur.substring(0,cur.indexOf('='));
}
}
return null;
}
This snippet only lists the supported ISO values. In my application I needed to pick the lowest ISO. Here is my solution:
private String getMinISO() {
if(ISOValues == null) {
return null;
}
String[] ISOarray = ISOValues.split(",");
Arrays.sort(ISOarray, myComparator);
String minISO = ISOarray[ISOarray.length-1];
return minISO;
}
Here myComparator is a class that compares two strings and sorts the array in descending order. All alphabet words are at the beginning and all numbers are at the end. Here is my implementation:
// Singelton class
public class MyComparator implements Comparator<String> {
private static MyComparator myComparator = null;
private MyComparator() {}
#Override
public int compare(String a, String b) {
return compareString(a,b);
}
public static int compareString(String a, String b) {
if (a.length() > b.length())
return -1;
if (a.length() == b.length()) {
if (a.compareTo(b) > 0)
return -1;
else if (a.compareTo(b) == 0)
return 0;
}
return 1;
}
public static synchronized MyComparator getInstance() {
if(myComparator==null) {
myComparator = new MyComparator();
}
return myComparator;
}
}
I hope my answer helps other people. :)
Cheers!
#ee3509
szias answer is correct.
Only
String supportedIsoValues = camParams.get("iso-values");
returns null on some devices.
Nevertheless you can set the iso by
Camera cam = Camera.open();
Camera.Parameters camParams = cam.getParameters();
camParams.set("iso", "400"); // values can be "auto", "100", "200", "400", "800", "1600"
cam.setParameters(camParams);
I do not know whether "800" or "1600" is supported on all devices
you can check what you did by
String newVAlue = camParams.get("iso");
Forgive my ignorance, but how is this different from "exposure compensation" set via setExposureCompensation()? Wikipedia has some of the conversion formulas you might find useful.