Extra line breaks at end of text - android

I seem to be getting what seems like some extra line breaks after using this method to set the text of a TextView
message.setText(Html.fromHtml( message ));
How can I remove these? They cause my layout to get warped since it adds two extra lines to the output.
The string was saved to my sqlite database via Html.toHtml( editText.getText() ).trim();
Initial string input : hello
Log output of the message variable: <p dir="ltr">hello</p>

you can use this lines ... totally works ;)
i know your problem solved but maybe some one find this useful .
try{
string= replceLast(string,"<p dir=\"ltr\">", "");
string=replceLast(string,"</p>", "");
}catch (Exception e) {}
and here is replaceLast ...
public String replceLast(String yourString, String frist,String second)
{
StringBuilder b = new StringBuilder(yourString);
b.replace(yourString.lastIndexOf(frist), yourString.lastIndexOf(frist)+frist.length(),second );
return b.toString();
}

For kotlin you can use
html.trim('\n')

Looks like toHtml assumes everything should be in a <p> tag. I'd strip off the beginning and ending <p> and </p> tags before writing to the database.

This is working as below in Kotlin.
val myHtmlString = "<p>Test<\/p>"
HtmlCompat.fromHtml(myHtmlString.trim(), FROM_HTML_MODE_COMPACT).trim('\n')

Related

How to check if a string has a specified character?

I am new to android studio and kotlin. I need to find a way to check if a string contains a char, which is, in this case, "/"
I want to form a piece of code in the following manner:
if (string input contains a character "/") = true {
<code>
}
else{
<code>
}
Please tell me how to do this, and if possible, give me the code I'll need to specify as the condition.
You can use contains, like this:
val a = "hello/"
val b = a.contains("/")
When the string has the character will return true.

How to Replacing process on a string that gets from Xml file

I get some string from xml as using XmlPullparser.
String test ;
if (name.equals("content"))
test = myParser.getAttributeValue(null, "value").toString();
Bythe way the xml side look like this
<content value = "sometext">
These are some part of my code. The code upperside returns 'sometext'. So simple. But when i try to some operations on this string value there is nothing changed.I mean
Textview tv = ....
tv.setText(test);
It seems what it is , textview's text happens "sometext".
After this i try to change some characters.
test.replace('e', 'a');
tv.setText(test);
It has to seem after this "somataxt", but it still seem "sometext". There is something wrong here :)
In your case you just replace the 'e' with 'a' but never assign return value from replace(char, char) to test String object:
test = test.replace('e', 'a');
tv.setText(test);
OR
tv.setText(test.replace('e', 'a'));

Remove garbage character from String

In my Android Application when I am reading the particular data from NFC chip it's giving garbage values as show follows which is printed on Log
����������������
I used following line to remove garbage value
str.replaceAll("[^\\p{ASCII}]", "")
but it is not working.
Please provide me solution.
That is because � is not an ASCII character. It is a unicode character with (int) � returning 65533.
And your code str.replaceAll("[^\\p{ASCII}]", "") works perfectly fine.
scala> val str ="����������������"
str: String = ����������������
scala> str.replaceAll("[^\\p{ASCII}]", "")
res8: String = ""
You need to show more code and explain what exactly you are trying to do.
Better to retrieve the data in the form of UTF-8 format then it helps. try it out.
or convert the string to UTF-8 format
i.e, String _data=new String(str.getBytes(),"UTF-8");
it returns the data in UTF-8 format
One solution using this method .replaceAll("[^\\x00-\\x7F]", "")
String str = "jorgesys���������������� was here!";
str = str.replaceAll("[^\\x00-\\x7F]", ""));
so the result of str is:
jorgesys was here!

Android Jsoup in service - get text of span

Im pretty new to jsoup. For days im trying now to read out a simple number from a span without any success.
I hope to find help here. My html:
<div class="navi">
<div class="tab mail">
<a href="/comm.php/indexNew/" accesskey="8" title="Messages">
<span class="tabCount">1 </span>
<img src="/b2/message.png" alt="Messages" class="moIcon i24" />
</a>
</div>
The class tabCount excists 3 times though in the whole document and I am interested in the first span with this class.
Now I am trying in onCreate() of a service to create a thread with:
Thread downloadThread = new Thread() {
public void run() {
Document doc;
try {
doc = Jsoup.connect("https://www.bla.com").get();
String count = doc.select("div.navi").select("div.tab.mail").select("a[href]").first().select("tabCount").text();
Log.d("SOMETHING", "test"+(count));
} catch (IOException e) {
e.printStackTrace();
}
}
};
downloadThread.start();
This forces my app to crash. The same if i change text() to ownText(). if i remove text() then the app can start but it gives me null.
what am i doing wrong? By the way, besides the service a webview is loading the same url. might that be a problem?
You only need to select the element you're interested in, you don't need to get every outer element before. In your example you could try
String count = doc.select("span.tabCount").text();
Where you define the type of the element "span" and class name ".tabcount"
For an example that might help you, look at this link
Edit:
Try this code instead, this will get the value of the first span.
Elements elements = doc.select("span.tabCount");
String count = elements.first().text();
And if you want to print all elements you could do like this.
Elements elements = doc.select("span.tabCount");
for (Element e : elements) {
Log.d("Something", e.text();
}
Haven't you meant .select(".tabCount")?
BTW, on Android AsyncTasks are more convenient than Threads. Also, empty catch blocks are a bad practice.
Your select statement is wrong. You can insert the whole selection string in one line. Furthermore you have to prefix "tabCount" with a dot as it is a class.
String count = doc.select("div.navi div.tab.mail a").first().select(".tabCount").text();

How to read word by word from file?

Could anybody post here some code how can I read word by word from file? I only know how to read line by line from file using BufferedReader. I'd like if anybody posted it with BufferedReader.
I solved it with this code:
StringBuilder word = new StringBuilder();
int i=0;
Scanner input = new Scanner(new InputStreamReader(a.getInputStream()));
while(input.hasNext()) {
i++;
if(i==prefNamePosition){
word.append(prefName);
word.append(" ");
input.next();
}
else{
word.append(input.hasNext());
word.append(" ");
}
}
There's no good way other than to read() and get a character at a time until you get a space or whatever criteria you want for determining what a "word" is.
If you're trying to replace the nth token with a special value, try this:
while (input.hasNext()) {
String currentWord = input.next();
if(++i == prefNamePosition) {
currentWord = prefName;
}
word.append(currentWord);
word.append(" ");
}
Another way is to employ a tokenizer (e.g. in Java) and using the delimiter space character (i.e. ' '). Then just iterate through the tokens to read each word from your file.
You can read lines and then use splits. There is no clear definition of word but if you want the ones separated by blank spaces you can do it.
You could also use regular expressions to do this.

Categories

Resources