Want to get the Special Character Parsed in the Response - android

I am getting the Special Character " ã " and a dash sign " - " in my Text Response originally and When I perform the setText() on that text it shows me " � " sign at both the Places in my Device and emulator.
I want to show what exactly I have in the response, I have no clue what I do to look like what it actually is. I have googled much and tried various stuff like UTF-8, ISO Encoding Standards and HTML.fromHTML and others, but all useless.
Does anyone have the answer for this.
Thanks
DavidBrown

You can save the character as an Object and then retrieve the object and set it in the textview.

you can replace these special character with their ascii code either at server side or at client side. see if this works

Related

URL encoding is getting failed for special character. #Android

I'm working on a solution where need to encode string into utf-8 format, this string nothing but device name that I'm reading using BluetoothAdapter.getDefaultAdapter().name.
For one of sampple I got a string like ABC-& and encoding this returned ABC-%EF%BC%86 instead of ABC-%26. It was weird until further debugging which helped to identify that there is difference between & and &. Second one is some other character which is failing to encoded as expected.
& and & both are different.
For encoding tried both URLEncoder.encode(input, "utf-8") and Uri.encode(input, "utf-8") but nothing worked.
This is just an example, there might be other character which may look like same as actual character but failed to encode. Now question are:
Why this difference, after all it is reading of some data from device using standard SDK API.
How can fix this be fixed. Find and replace with actual character could be a approach but scope is limited, there might be other unknown character.
Any suggestion around !!
One solution would be to define your allowed character scope. Then either replace or remove the characters that fall outside of this scope.
Given the following regex:
[a-zA-Z0-9 -+&#]
You could then either do:
input.replaceAll("[a-zA-Z0-9 -+&#]", "_");
...or if you don't care about possibly empty results:
input.replaceAll("[a-zA-Z0-9 -+&#]", "");
The first approach would give you a length-consistent representation of the original Bluetooth device name.
Either way, this approach has worked wonders for me and my colleagues. Hope this could be of any help 😊.

How to return apostrophe when using Google Translate API for Android?

I have an Android application that uses Google Translate API.
Everything works great, including when I tried to translate phrases that include apostrophe like "We've eaten" to Spanish.
However, problems occur when the translation result I should be getting back contains an apostrophe. For example, when I translate a Spanish phrase, "A ver", into English, it returns "Let&#39s see" with a ";" after "9". It seems like whenever I have a phrase that should return an apostrophe, it returns "&#39" with a ";" after "9". (Not placing ";" after "9" because it gets converted to an apostrophe by stackoverflow).
I can think of a way to solve it. After I get the translation result, I can match the string for ""&#39" + ";" and replace it with an apostrophe.
However, I don't feel like this is the way I should approach it. It's very unlikely that a user will actually type in "&#39" as an input for translation, but hard coding a manual conversion like this seems like it might cause problems down the road. I'll love to hear your thoughts on this.
Please let me know how I should fix/approach this issue.
Thank you!
The best solution is to add &format=text to your query.
You are correct hard codding is not solution,
But you can convert this HTML entity back to apostrophe, by Using HTML classes provided already.
Html.fromHtml((String) "Let's see").toString()
Above code will convert any valid HTML entity.
I Hope this is what you are looking for.
Thanks Guillaume. For those using php.
$translation = $translate->translate($stringToTranslate, ['target' => $target, 'format' => 'text']);
Thanks Guillaume. For those using go. (api v3)
req := &translatepb.TranslateTextRequest{
MimeType: "text/plain", // add this line to request
}

How to replace the characher `\n` as a new line in android

I have one server response for an API request as shown below.
Success! Your request has been sent.\n\nWe’ll inform you once it is done.
This message I need to show in a Snackbar. I need new line to be added in the place of \n in this response . I tried by using replaceAll like
String message = (serverResponse.getMessage()).replaceAll("\\n", System.getProperty("line.separator"));
but it is showing like this
Same message if I add in string.xml resource file and get using getString(R.string.message) then the \n is working properly. How can I get a new line from this response string?
I tried changing \n with other character like <new_line> from server response and it is working fine with replaceAll. Problem is only with \n in response message. Is there any way to parse \n?
What you need is
String message = serverResponse.getMessage().replaceAll("\\\\n", "\n");
Why four backslashes are needed?
Because in Java backslash \ is escape character. If you want to have single backslash literal inside Java string you have to escape it and use \\
But, replaceAll method expects regex expression, where again backslash is escape character so you need to escape it, too.
Basically, in above code Java string parser will first convert those four backslashes to two \\\\ -> \\ and then regex parser will interpret remaining two backslashes as single backslash literal.
I believe you should be able to accomplish by doing the following.
// Replace "\n" with "<br>"
String message = (serverResponse.getMessage()).replaceAll("\\n", "<br>");
// Now set SnackBar text using HTML
mSnackBar.setText(HTML.fromHTML(message))
By using HTML.fromtHTML(String) you should be able to keep any formatting, such as breaks, ASCII HTML characters (bullets, stars, ect.), coloring and/or bolding/italicizing! I use this quite often to format text in TextViews that I have displayed to users. Do not see why it wouldn't work with SnackBars!
The Support Design Library will force only 2 lines for the Snackbar. This correlates to around 80dp max size.
Your solution should work, and is correct. Try it out in a Toast for a quick test. It will work as your expect. Another test you can do is to get rid of one of the \n, then it will probably display correctly; however, there are a few other options you can do for. Again, these are just tests. Check below for some real solutions!
Solutions
Remove all the \n from the Snackbar text. This is probably the best solution as it will allow your design to remain as close to Material as possible. Highly Recommended
You can get the actual TextView from the Snackbar, and modify its max number of lines
View sbv = snackbar.getView();
TextView tv = (TextView) sbv.findViewById(android.support.design.R.id.snackbar_text);
tv.setMaxLines(5);
In XML, you can modify the attribute that affects the Design Library's Snackbar number of lines. Not Recommended at all. This name can change without notice and break your UI
<integer name="design_snackbar_text_max_lines">5</integer>
Edit
If you have access to modify the contents of the server response, then I Highly highly highly suggest that you modify the returned server response to be way more concise to the user. Your current message is not concise and takes the user longer than needed to read.
Change it to this..
Request sent! You will be informed shortly.
I would actually find a better work for Request if you can. For example, if they ordered pizza and sent a request, then you ould say Order sent! .... Also, you might need to modify shortly to be more accurate to what a user can expect. Shortly, to me, means I should expect something within the hour at the very latest.
Anyways, check out this documentation. It is higly recommended for writing styles on Android. https://www.google.com/design/spec/style/writing.html#writing-language
Source: Android Multiline Snackbar
Snackbar snackbar = Snackbar.make(ref_id, "Success! Your request has been sent.\n\nWe’ll inform you once it is done.",
Snackbar.LENGTH_LONG).setDuration(Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView tv= (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
tv.setMaxLines(3);
snackbar.show();
How about this, Jrd.
strings.xml :
<string name="br">\n</string>
snackbar :
"Your request has been sent.." + getResources().getString(R.string.br)
StringBuilder strAppend = new StringBuilder();
strAppend.append("\n");
String newString = oldString.replace("\n", strAppend);
Log.d(TAG, "new: " + newString );

Create regular expression using android pattern

I have a sample message . I need to create a regular expression to validate using android pattern.
sample message :
ERR|any digit|any digit;
checking validation:
1.Starting fixed characters :ERR
separator character :|
digit after | character
Message termination ;
I have tried like this way:^{ERR}+{|}+\d+{|}+\d+{;}$
Am I right? Please help to solve my problem.
The corrected regex you gave would be ^(ERR)+(\\|)+\\d+(\\|)+\\d+;$. Brackets are used for grouping, not braces. Also, in regex, + is used to represent "one or more of the previous expression". So writing (ERR)+ means "one or more of the string 'ERR'", so strings like "ERRERR|123|456;" would be matched (same thing goes for the pipe characters) - this is not what you are trying to do, I assume.
Having said that, try this: "^ERR\\|\\d+\\|\\d+;$"

Still having problem with imported text from my database to Android

I have had a problem with formattings symbols shown on the image. I got help from this site and I used Html.fromHtml() and hope that it would use the formatting, the only thing it did was to remove the formatting symbol but not anything else like newline or so.
If I cant find a way to use the formatting I wonder if it is possible to add a "\n" everytime the symbol is shown. The thing is if I try to use a method like
(Ps the "[]" is used here instead of the real symbol cause I cant find out how to write it.
int nr=theTextString.indexOf("[]")
and then replace the text with
theTextString=theTextString.substring(0,nr)+ "\n" + theTextString.substring(nr);
but the problem is that the symbol is not represented as a character to look for the index at, not in any way I know of. Would really be thankful for help.
If [] crap character is only shown on the last chacter do this
Log.i("My String before:", theTextString);
theTextString = theTextString.substring(0, theTextString.length()-2);
Log.i("My String after:", theTextString);
Btw: [] <-- is not the same character (Infact, there are two characters) it is something else.
Solution: To encode the string
theTextString = URLDecoder.decode(theTextString, HTTP.UTF-8);

Categories

Resources