How to get "hour", "hours" string in android - android

I want to format time duration like "1 hour", "2 hours" in my app and also i don't want to store this "hour" strings in my strings.xml because there is 8 languages in my app so i am looking for any existing solution.
Best method that i found was DateUtils.getRelativeTimeSpanString() but it gaves me: "in 1 hour", "in 2 hours". Just cut off this "in" doesn't appears like good solution for me.
I already had looked at android.R.plurals but not found anything.
I believe that i can get this "1 hour" string from native method since I can get "in 1 hour"

Related

How to change DateUtils.getRelativeTimeSpanString strings?

I'm trying to convert a date to relative date.
For example
12-03-2017 --> 2 years ago.
I'm using an android class which is "DateUtils.getRelativeTimeSpanString"
This class works just fine. But I have a little problem.
My app will serve in Turkish language. But method returns some very old sentence.
When relative time is equal to "2 days ago" class must return "2 gün önce" which is literal translation of "2 days ago". But instead it returns "Evvelsi gün" Which has same meaning but old sentence and I don't want to use that
How to change this string for Turkish language.
I did this:
if(DateUtils.getRelativeTimeSpanString(list.get(position).getCreatedAt().getTime(), System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS).toString().toLowerCase().equals("evvelsi gün")){
//if class retuned unwanted word then dont use it but use preferred word.
holder.postdate.setText("2 gün önce");
}
else{
//if class returned preferred word use it
holder.postdate.setText(DateUtils.getRelativeTimeSpanString(list.get(position).getCreatedAt().getTime(), System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS));
}
But as you can see I'm setting textView's text with hardcoded strings. Is this a good solution? Is there any more professional ways?

What is the one line max length limit in XML files?

Recently I've faced an issue related to max line (string) length limit in a path attribute of vector xml. In my case 51k chars appears to be an overlimit while 29k is OK. Anyway, I guess it is not related to some specific kind of xml like manifest, string, style, attr, drawable, layout or any other, I'm sure it is a general limitation for any xml in Android project.
So I'm curious what is the limit (I mean an exact max number of chars allowed for one line in Android xml) and why its not being applied by AndroidStudio at vector importing time? It looks like Google does not know about this limit itself or maybe its a limitation of a gradle.
Please do not point me that long strings are bad - I do realize it and it does not cancel my question.
Guys, please read my question and other SO questions you might think are the dupes carefully! There is no answer to my question there.
Android strings.xml length SO question has absolutely other meaning. Asker asks if there is a way to get the count of strings in strings.xml. Besides that, my question is not related to strings.xml exactly but to any xml file at all.
Android IndexOutOfBound Exception for resource getString is about absolutely another exception/issue and gives absolutely NO answer on my question. Again, my question is not related to strings.xml at all.
What is the maximum amount of data that a String can hold in java? is not what I am asking about, its about Java String class limitation. Even if its related then the limit is about Integer.MAX/2(cuz for Unicode it takes 2 bytes to encode 1 char) so its not 51k chars in any way.
My guess reading your question was that the limit would be somewhere between 29k and 51k but clipped to a logical number
I might have found the answer by testing it.
As you said, a char is stored on two bytes. So 29k char would be 58k bytes and 51k would be 102k bytes.
The "logical" limit would be 65536 as it is 2^16. so the limit in char is 2^16 / 2 or 2^15 which is 32768
I tested to put strings in my string.xml (basically a long line of 'a')
<string name="length_test32000">(32 000 a)</string>
<string name="length_test32767">(32 767 a)</string>
<string name="length_test32768">(32 768 a)</string>
<string name="length_test32769">(32 769 a)</string>
<string name="length_test33000">(33 000 a)</string>
Then i tried to log their size :
String test32k = getString(R.string.length_test32000);
String test32k767 = getString(R.string.length_test32767);
String test32k768 = getString(R.string.length_test32768);
String test32k769 = getString(R.string.length_test32769);
String test33k = getString(R.string.length_test33000);
Log.i("tag", "32000 : "+test32k.length());
Log.i("tag", "32767 : "+test32k767.length());
Log.i("tag", "32768 : "+test32k768.length());
Log.i("tag", "32769 : "+test32k769.length());
Log.i("tag", "33000 : "+test33k.length());
Here are the results :
I/tag: 32000 : 32000
I/tag: 32767 : 32767
I/tag: 32768 : 16
I/tag: 32769 : 16
I/tag: 33000 : 16
From 32768 it seems to be truncated so i log what was inside
Log.i("tag", "32768 : "+test32k768.length() + " content : " + test32k768);
And the result is :
I/tag: 32768 : 16 content : STRING_TOO_LARGE
The maximum char seems to be 32767 (2^15 - 1) characters. I didn't find any official doc that say that, but it is what i found while testing

Make voice pause between phrases on Android (TalkBack and Text-To-Speech)

I am setting the content description for a TextView using setContentDescription method.
The string passed to this method is a concatenation of two sentences separated by a period "."
When TalkBack ot TTS (Text-To-Speech) read this, it does not mark a pause between the two sentences.
My question is, is there a some way to handle this, a special UTF character for example ?
Try a newline charachter.
To read this barcode: "3S REGR 2345", I found that "3 S\nR E G R\n2 3 4 5" got me the result I was after on Samsung TTS.
It will be read as "Three S. R E G R. Two Three Four Five"
Uncertain if there is any special character available, but another solution might be to split the speech and add
textToSpeech.playSilence(750, TextToSpeech.QUEUE_ADD, null);
then continue with the rest of the split

geting error/warning for plurals : "The quantity 'one' matches more than one specific number..."

Background
I work on an app that has many translations inside it.
I have the next English plural strings:
<plurals name="something">
<item quantity="one">added photo</item>
<item quantity="other">added %d photos</item>
</plurals>
and the French translation:
<plurals name="something">
<item quantity="one">a ajouté une photo</item>
<item quantity="other">a ajouté %d photos</item>
</plurals>
The problem
For both the French and Russian, I get the next warning:
The quantity 'one' matches more than one specific number in this
locale, but the message did not include a formatting argument (such as
%d). This is usually an internationalization error. See full issue
explanation for more.
when choosing to show details , it says:
Thins is, I don't get what should be done to fix it, and if there is even a problem...
The question
What exactly should I do with those strings? What should I tell the translators?
In French singular form is used when count is 0 or 1. In English singular form is used only when count is 1. 0 uses a plural form.
This is why you need to insert a placeholder (%d) in your French singular pattern.
I'm going to write an answer since this is quite an difficult explanation.
In various languages, nouns use the singular form if they end with 1. Refer to: http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html
Explaining in English there are languages where it's correct to say "added 1 photo" as well as "added 101 photo". Notice the "photo". So this means that you should always add "%d" on one strings as well. Android will choose the best scenario to use. Which means that in English it will choose "other" for numbers > 1 and on other languages it will choose "one" for numbers ended in one.
Resuming, add %d to your one string and should be fine. Also make sure your translators respect the plural rules for their language.

Android Copy String Like Delphi Copy's Function

Example 1:
i Have an Apple
Example 2:
I Love U
i would like to copy 10 string only, start index from first character.
Delphi code would be like this :
copy('I Have an Apple',0,10)
copy('I Love U',0,10)
Result become
i Have an
I Love U
Any same function in Android?
no method how long the string, i just want the first 10 character
You're probably after .substring(int start,int end) which applied to your code, would be something like the code below, however when you say you would like to copy "10 string only" you should say 10 characters from said string:
String apple = "i Have an Apple";
String appleCopy = apple.substring(0,10); // "i Have an "
If you want to handle the IndexOutOfBoundsException inline, you could do this, as suggested here, which would take the first n characters if the length is sufficient, or the whole string if its shorter.
String appleCopy = apple.substring(0, Math.min(apple.length(), 10));

Categories

Resources