how to find and replace character in textview in android? - android

I use this code for replace this • character with \n in textview in android
TextView tvcontent=(TextView) row.findViewById(R.id.row_comment_content);
tvcontent.setText(content[position].replace("•", "\n"));
Now I want to replace this char in the image http://i.stack.imgur.com/cnJeI.jpg
but I don't know what is the ASCII code of that char in the image to replace in android.
If you know what is ASCII code of the char please help.

If you mean a middot char, its code is 183.
Also, maybe this link can help.
Ok, now I got what you want.
This page would answer your question. Brief extract from this page:
this is OBJECT REPLACEMENT CHARACTER and it's code in Java is \uFFFC.
Also I typed it in Android Studio - it works (screen below):
If you possibly need it in HTML - it's code is  ().
And don't forget to use single quote when you will replace this char!

Use this code:
String a = "your content with dots";
String b = a.replace("dots", "\n");
textview.settext(b);

Try running this code to find the ascii value of any character. But for this bullet I don't think there is any ascii code.
public class HelloWorld{
public static void main(String []args){
String s= "•";
int a = s.charAt(0);
System.out.println("Ascii code is "+ a);
}
}
Here when we run this code then compiler shows an error:
unmapped character for encoding ascii.
But in future if you want to know ascii code of any character that is actually ascii, just use this function.

Related

JSON string to TextView newline

First of all, I have gone through questions similar to the problem I am facing and those solutions are not working for me.
I have a TextView field on my Android app which is supposed to display multiple paragraphs i.e multiple new lines. I am getting this string from a database present in my online server as a JSON.
The text contains \n in it and I am expecting it to create new lines once it is received by the app. But it displays the whole text without any breaks along with "\n" character.
Below is the text present in my database.
First line. \nSecond line. \nThird line.
JSON string received by me inside the app.
{
"server_response": [{
"news_expand": "First line. \\nSecond line. \\nThird line."
}]
}
Code to extract string from JSON. I have left out the code to get get JSONArray and JSONObject for simplicity.
na_expand = gna_jo.getString("news_expand");
String extracted from the JSON. Got this by printing the na_expand string.
First line. \nSecond line. \nThird line.
Code to display the text in the TextView. Note the below 'na_expand' is an SparseArray present in a different activity hence the 'get(position)' code.
art_expand.setText(na_expand.get(position));
Below is the text I get on the emulator.
First line. \nSecond line. \nThird line.
What am I doing wrong here?
I think you should replace \n with \n in your string before setting test to your textview same below
b= b.replaceAll("\\n","\n");
So I found a workaround to the problem. As I was not sure where the issue was happening with \n, I modified my text present in the database to have a symbol other than \n. For eg: ~
First line.~Second line.~Third line.
You can use a website like this - https://www.gillmeister-software.com/online-tools/text/remove-line-breaks.aspx to replace the line breaks with any symbol you want.
Next, I used the StringSplitter class to break the string received in JSON and then again join it together with \n.
String joined;
String expand_temp = na_expand.get(position);
TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter('~');
splitter.setString(expand_temp);
StringBuilder stringBuilder = new StringBuilder();
for (String s_temp : splitter) {
stringBuilder.append(s_temp + "\n");
}
joined = stringBuilder.toString().trim();
This worked! I used this string in setText.
art_expand.setText(joined);
Try below code
myTextView.setText(Html.fromHtml("yourString with additional html tags"));
It will resolve all the html tags accordingly and effects of the tags will be reflected as well.
NOte: For devices greater than Nougat use below code
myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
Hope that helps
The \ character is an escape character in JSON. So, when you get \\n, it actually means \n, not the newline character, which should have been just \n. So what you see is an expected behaviour. The JSON you get should have ideally been:
{
"server_response": [{
"news_expand": "First line. \nSecond line. \nThird line."
}]
}
Get your server to respond properly, otherwise you'll have to strip the unnecessary \.
Do you haveandroid:singleLine="true" on your TextView? If yes it will ignore the \n and will place the text in a single line.
You can just add replaceAll("\\n","\n") when you set value to your art_expand EditText. It should be:
art_expand.setText(na_expand.get(position).replaceAll("\\n","\n"));

I need to test if a string is an Hexadecimal value

I'm starting with Android Studio and making a simple App that take an input from am EditText and check if have four characters long(the easy part) and if the whole string is an hexadecimal value like "FFA1".
I think in to add the "0x" to the String but that use more resources a far a read in another post/questions.
I also read that exist a library called TextUtils with a function isDigitsOnly() but I'm not sure if they call digits to 0-9 values or 0-9 and A-F too.
In VB.NET I use to add &H to the String and use isNumeric() function to detect Hexa values, but in android I'm lost.
Can somebody enlighten me?
Thanks.
You should to test with this void:
private void test(){
try{
String hex = "Your String"
int hexadecimalValue = Integer.parseInt(hex, 16);
System.out.println("valid hexadecimal");
} catch(NumberFormatException e){
// not a valid hex
System.out.println("not a valid hexadecimal");
}}

Using Jsoup is it possible to remove text characters after white space?

Using Jsoup is it possible to remove text characters after whitespace?
For example:
<td> 4.9 ft</td>
Is it possible to remove the "ft" from the result?
Thank you.
Jsoup will not help you with that. However, you can parse the Element(s) into a String, and then replace part of the string with another. An example is below:
String parsedstring = YourElement.text();
String replacedstring = parsedstring.replace(" ft","");
Here's another question that may help you: Android - how to replace part of a string by another string?
Try this:
1) Save the text as String.
2) Get the length of the String, then use the substring method to remove the last two characters.
Here's an example
String result = Element.text();
int resultLength = result.length();
result = result.substring(0, resultLength -2);
Please note: This is a beginner's advice.

Setting Danish Character Text

I am getting some strings from json. My string contains special characters like "æ" from Næstved an many more like "ø" from køkken. But When I set Text these strings to ant textview, I get my strings printed in unusual way.
Example: For køkken I get k&oslash ;kken.
I think I need to encode or decode my string somewhere but where I don,t know.
Please help.
Thanks in advance
The displayed version of your string represents an HTML encoded entity. You might want to verify that it is not coming in this way in your JSON data, but in any case, to decode it you can use the StringEscapeUtils.unescapeHtml4 method from Apache Commons Lang:
final String escaped = "køkken";
System.out.println(StringEscapeUtils.unescapeHtml4(escaped));
Output:
køkken
Did you check out the Latin Coding for your characters? I know the Ash character can be coded with æ and will show up æ in the browser.
Here is the a list of codes
Hope this helps!

android java URLDecoder problem

i have a String displayed on a WebView as "Siwy & Para Wino"
i fetch it from url , i got a string "Siwy%2B%2526%2BPara%2BWino". // be corrected
now i'm trying to use URLDecoder to solve this problem :
String decoded_result = URLDecoder.decode(url); // the url is "Siwy+%26+Para+Wino"
then i print it out , i still saw "Siwy+%26+Para+Wino"
Could anyone tell me why?
From the documentation (of URLDecoder):
This class is used to decode a string which is encoded in the application/x-www-form-urlencoded MIME content type.
We can look at the specification to see what a form-urlencoded MIME type is:
The form field names and values are escaped: space characters are replaced by '+', and then reserved characters are escaped as per [URL]; that is, non-alphanumeric characters are replaced by '%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks, as in multi-line text field values, are represented as CR LF pairs, i.e. '%0D%0A'.
Since the specification calls for a percent sign followed by two hexadecimal digits for the ASCII code, the first time you call the decode(String s) method, it converts those into single characters, leaving the two additional characters 26 intact. The value %25 translates to % so the result after the first decoding is %26. Running decode one more time simply translates %26 back into &.
String decoded_result = URLDecoder.decode(URLDecoder.decode(url));
You can also use the Uri class if you have UTF-8-encoded strings:
Decodes '%'-escaped octets in the given string using the UTF-8 scheme.
Then use:
String decoded_result = Uri.decode(Uri.decode(url));
thanks for all answers , i solved it finally......
solution:
after i used URLDecoder.decode twice (oh my god) , i got what i want.
String temp = URLDecoder.decode( url); // url = "Siwy%2B%2526%2BPara%2BWino"
String result = URLDecoder.decode( temp ); // temp = "Siwy+%26+Para+Wino"
// result = "Swy & Para Wino". !!! oh good job.
but i still don't know why.. could someone tell me?

Categories

Resources