I have an EditText to get description and want to set max length 145 chars.
I have set maxlength 145 in XML. This works correctly for English.
But for languages other than English, being specific, Marathi, Hindi etc it doesn't work properly. I counts each symbol as separate character.
For example: "व्ही"
this is considered as one character in Hindi, and it's length should be 1, but it returns me length 2.
I have seen the solution in this link
But it doesn't work for Hindi/Marathi language because it cannot detect connected characters. How to detect connected characters?
Thanks in advance :)
Plz try this code
String str = "व्ही";
int count = 0;
for(int i=0; i<str.length(); i++)
{
if(!isMark(str.charAt(i)))
count++;
}
textview.setText(count);
Try this:
import java.text.BreakIterator;
import java.util.Locale;
public class MyClass {
private int graphemeClusterCount(Locale locale) {
String text = "व्ही";
BreakIterator breakIterator = BreakIterator.getCharacterInstance(locale);
breakIterator.setText(text);
int count = 0;
int start = breakIterator.first();
for (int end = breakIterator.next();
end != BreakIterator.DONE;
start = end, end = breakIterator.next()) {
count++;
}
return count;
}
}
Some references:
Unicode FAQ on what it calls grapheme clusters: http://unicode.org/faq/char_combmark.html#7
Sample code from Android documentation: https://developer.android.com/reference/java/text/BreakIterator.html
ICU has a BreakIterator implementation, too: http://site.icu-project.org/ and you can import in Gradle:
compile 'com.ibm.icu:icu4j:58.2'
Related
I have a big paragraph which may have numbers, email addresses and links. So I have to set setAutoLinkMask(Linkify.PHONE_NUMBERS | Linkify.EMAIL_ADDRESSES | Linkify.WEB_URLS) for my textview.
The content may contain digits of varying numbers. I want to set numbers having atleast 8 digits as phone number links.(For Eg : 12345678)
Is it possible to set minimum length for Linkify.PHONE_NUMBERS ?
Is there anyway to achieve this?
In case you can use Linkify.MatchFilter to specify minimum length or your some other requirements. There is not any direct way provided by Android.
Also somewhere in this SO post found some good examples.
use below pattern :
SpannableString buffer = new SpannableString(text);
Pattern pattern = Pattern.compile("^[0-9]\d{7,9}$");
Linkify.addLinks(buffer , pattern,"");
Yes, its possible. I researched this phenomenon :-)
To set the minimum length for a phone number, use this code:
private final Linkify.MatchFilter matchFilterForPhone = new Linkify.MatchFilter() {
#Override
public boolean acceptMatch(CharSequence s, int start, int end) {
int digitCount = 0;
for (int i = start; i < end; i++) {
if (Character.isDigit(s.charAt(i))) {
digitCount++;
if (digitCount >= 6) { // HERE: number 6 is minimum
return true;
}
}
}
return false;
}
};
To properly format and link phone numbers, use:
final SpannableString s = new SpannableString(myTekst);
Linkify.addLinks(s, android.util.Patterns.PHONE, "tel:", matchFilterForPhone, Linkify.sPhoneNumberTransformFilter);
Now place the formatted s in your TextView, and call:
findViewById(R.id.message).setLinkTextColor(Color.BLUE);
findViewById(R.id.message).setMovementMethod(LinkMovementMethod.getInstance());
That's all. Thanks for vote.
I was wondering how I could programmatically edit strings in android. I am displaying strings from my device to my website, and the apostrophes ruin the PHP output. so in order to fix this, I needed to add character breaks, ie: the backslash '\'.
For example, if I have this string: I love filiberto's!
I need android to edit it to: I love filiberto\'s!
However, each string is going to be different, and there will also be other characters that I have to escape from . How can I do this?
I was wondering how I could programmatically edit strings in android. I am displaying strings from my device to my website, and the apostrophes ruin the PHP output. so in order to fix this, I needed to add character breaks, ie: the backslash '\'.
This is what I have so far, thanks to ANJ for base code...:
if(title.contains("'")) {
int i;
int len = title.length();
char[] temp = new char[len + 1]; //plus one because gotta add new
int k = title.indexOf("'"); //location of apostrophe
for (i = 0; i < k; i++) { //all the letters before the apostrophe
temp[i] = title.charAt(i); //assign letters to array based on index
}
temp[k] = 'L'; // the L is for testing purposes
for (i = k+1; i == len; i++) { //all the letters after apostrophe, to end
temp[i] = title.charAt(i); //finish the original string, same array
}
title = temp.toString(); //output array to string (?)
Log.d("this is", title); //outputs gibberish
}
Which outputs random characters.. not even similar to my starting string. Does anyone know what could be causing this? For example, the string "Lol'ok" turns into >> "%5BC%4042ed0380"
I am assuming you are storing the string somewhere. Lets say the string is: str.
You can use a temporary array to add the '/'. For a single string:
int len = str.length();
char [] temp = new char[len+1]; //Temporary Array
int k = str.indexOf("'"), i; //Finding index of "'"
for(i=0; i<k-1; i++)
{
temp[i] = str.charAt(i); //Copying the string before '
}
temp[k] = '/'; //Placing "/" before '
for(i=k; j<len; j++)
{
temp[i+1] = str.charAt(i); //Copying rest of the string
}
String newstr = temp.toString(); //Converting array to string
You can use the same for multiple strings. Just make it as a function and call it whenever you want.
The String API has a number of API calls that could help, for example String.replaceAll. But...
apostrophes ruin the PHP output
Then fix the PHP code rather than require "clean" input. Best option would be to select a well supported transport format (say JSON or XML) and let the Json API on each end handle escape code.
I have a SMS counter that count typed character;
I wanna when user type English char(English chars + sings + numbers) ,counting Variable do ++ to Number: 160,
But , if in all there r a Persian char , counting Variable do ++ to Number: 70
what I should to do?
I Fined that this code : var ucs2 = text.search(/[^\x00-\x7D]/) in php return What character user ; Persian char or English.
The equivalent code in Java - What is Android؟
private final TextWatcher TextWatcher_Method = new TextWatcher()
{
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
cnt2=matn_counter.length();
TextView counter=(TextView)findViewById(R.id.txt_counter);
cnt1=(int) Math.ceil(cnt2/6);
Integer cnt3=(cnt2%6);
counter.setText(Integer.toString(cnt1)+"/"+Integer.toString(cnt3));
}
}
You can use Bidi class.
for more info look at this answer please.
You can't send 'real' SMS from emulator to device, because you don't have a SIM card.
You can, however, send it from emulator to emulator if you have more than one running.
To do this, check this question: Sending and receiving text using android emulator
Here's some useful tips on how to send SMS, make call, etc... http://www.apurbadebnath.com/blog/android-emulator-tips/
The Answer:
Actually you should check ASCII chars.
If any char is out of ASCII range (0x0000 to 0x007F) the message becomes to 70 length limit.
And otherwise as you motioned, if all chars is ASCII (for example English letters or numbers) the limitation on message length is 160.
the code is simple:
public static boolean isASCIIMessage(String s) {
for (int i = 0; i < Character.codePointCount(s, 0, s.length()); i++) {
int c = s.codePointAt(i);
if (c >= 0x0000 && c <=0x007F)
return false;
}
return true;
Anyway if you want detect Persian input see this answer.
Is there something I can do to make the text look in small caps/capital? As described here: http://en.wikipedia.org/wiki/Small_caps. I used a converter but some characters are missing.
EDIT 2015-08-02: As of API 21 (Lollipop) you can simply add:
android:fontFeatureSettings="smcp"
to your TextView declaration in XML, or at runtime, invoke:
textView.setFontFeatureSettings("smcp");
Of course, this only works for API 21 and up, so you'd still have to handle the old solution manually until you are only supporting Lollipop and above.
Being a bit of a typography geek at heart, this seemed like a really good question. I got to learn some more about Unicode today, as well as an answer for your question. :)
First, you'll need to have a font that includes "actual" small-caps characters. I'm assuming you know that since you're asking, but typically most professional fonts include these. Unfortunately most professional fonts are not licensed for distribution, so you may not be able to use them in your application. Anyway, in the event that you do find one (I used Chaparral Pro as an example here), this is how you can get small caps.
From this answer I found that the small caps characters (for A-Z) are located starting at Unicode-UF761. So I built a mapping of these characters:
private static char[] smallCaps = new char[]
{
'\uf761', //A
'\uf762',
'\uf763',
'\uf764',
'\uf765',
'\uf766',
'\uf767',
'\uf768',
'\uf769',
'\uf76A',
'\uf76B',
'\uf76C',
'\uf76D',
'\uf76E',
'\uf76F',
'\uf770',
'\uf771',
'\uf772',
'\uf773',
'\uf774',
'\uf775',
'\uf776',
'\uf777',
'\uf778',
'\uf779',
'\uf77A' //Z
};
Then added a helper method to convert an input string to one whose lowercase letters have been replaced by their Small Caps equivalents:
private static String getSmallCapsString (String input) {
char[] chars = input.toCharArray();
for(int i = 0; i < chars.length; i++) {
if(chars[i] >= 'a' && chars[i] <= 'z') {
chars[i] = smallCaps[chars[i] - 'a'];
}
}
return String.valueOf(chars);
}
Then just use that anywhere:
String regularCase = "The quick brown fox jumps over the lazy dog.";
textView.setText(getSmallCapsString(regularCase));
For which I got the following result:
Apologies for dragging up a very old question.
I liked #kcoppock's approach to this, but unfortunately the font I'm using is missing the small-cap characters. I suspect many others will find themselves in this situation.
That inspired me to write a little util method that will take a mixed-case string (e.g. Small Caps) and create a formatted spannable string that looks like Sᴍᴀʟʟ Cᴀᴘs but only uses the standard A-Z characters.
It works with any font that has the A-Z characters - nothing special required
It is easily useable in a TextView (or any other text-based view, for that matter)
It doesn't require any HTML
It doesn't require any editing of your original strings
I've posed the code here: https://gist.github.com/markormesher/3e912622d339af01d24e
Found an alternative here Is it possible to have multiple styles inside a TextView?
Basically you can use html tags formatting the size of the characters and give a small caps effect....
Just call this getSmallCaps(text) function:
public SpannableStringBuilder getSmallCaps(String text) {
text = text.toUpperCase();
text = text.trim();
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
if (text.contains(" ")) {
String[] arr = text.split(" ");
for (int i = 0; i < arr.length; i++) {
spannableStringBuilder.append(getSpannableStringSmallCaps(arr[i]));
spannableStringBuilder.append(" ");
}
} else {
spannableStringBuilder=getSpannableStringSmallCaps(text);
}
return spannableStringBuilder;
}
public SpannableStringBuilder getSpannableStringSmallCaps(String text) {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(
text);
spannableStringBuilder.setSpan(new AbsoluteSizeSpan(36), 0, 1, 0);
spannableStringBuilder.setSpan(new StyleSpan(Typeface.BOLD), 0, 1, 0);
spannableStringBuilder.setSpan(new StyleSpan(Typeface.BOLD), 1,
text.length(), 0);
return spannableStringBuilder;
}
This is not my code but its works perfectly.
public SpannableString getSmallCapsString(String input) {
// values needed to record start/end points of blocks of lowercase letters
char[] chars = input.toCharArray();
int currentBlock = 0;
int[] blockStarts = new int[chars.length];
int[] blockEnds = new int[chars.length];
boolean blockOpen = false;
// record where blocks of lowercase letters start/end
for (int i = 0; i < chars.length; ++i) {
char c = chars[i];
if (c >= 'a' && c <= 'z') {
if (!blockOpen) {
blockOpen = true;
blockStarts[currentBlock] = i;
}
// replace with uppercase letters
chars[i] = (char) (c - 'a' + '\u0041');
} else {
if (blockOpen) {
blockOpen = false;
blockEnds[currentBlock] = i;
++currentBlock;
}
}
}
// add the string end, in case the last character is a lowercase letter
blockEnds[currentBlock] = chars.length;
// shrink the blocks found above
SpannableString output = new SpannableString(String.valueOf(chars));
for (int i = 0; i < Math.min(blockStarts.length, blockEnds.length); ++i) {
output.setSpan(new RelativeSizeSpan(0.8f), blockStarts[i], blockEnds[i], Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
return output;
}
Example:
SpannableString setStringObj = getSmallCapsStringTwo("Object"); tvObj.setText(setStringObj);
in XML
edit text has property :android:capitalize=""
I have a List of Strings and i want to compare every i write in an EditText with that list. If there is a match then i have to add a "-" character as a prefix for that word.
I am using a TextWatcher and this is my code so far:
#Override
public void afterTextChanged(Editable s) {
String tmp = s.toString();
words = tmp.split(" ");
for (int i = 0; i < words.length; i++) {
for (Iterator iterator = myList.iterator(); iterator
.hasNext();) {
String str = (String) iterator.next();
if (str.equalsIgnoreCase(words[i])) {
if (!words[i].contains("-")) {
tmp = tmp.replace(words[i], "-" + words[i]);
}
editMain.setText(tmp);
editMain.setSelection(tmp.length());
}
}
}
}
It works but if i type the same word twice in my EditText, the first ocurrence gets two "--".
For example:
hello this is -android (works ok)
hello this is --android -android (does not work ok)
And the desired result should be:
hello this is -android android (because the repeated word already exists)
Any help? thanks in advance
Your question is not very clear. Maybe you mean android word has already been found and then it should not be prefixed by a -.
If that's the case, just remove a mathcing word from mylist. For that use a listIterator.
try to set a counter. If the counter is bigger than 1, then don't write the -