I want to display text inside textview using this code:
Html.fromHtml("<html><body><table style=width:100%><tr><td><B>No</td><td><B>Product Name</td><td><B>Qty</td><td><B>Amount</td></tr></body></html>");
But result is not in correct format result look like this:
NoPRoductNameQtyAmount
please suggest what i am doing wrong in this code.
fromHtml() does not support <table> and related tags. Your choices are:
Reformat your text to avoid tables
Use WebView to render your HTML table
Use native widgets and containers (e.g., TableLayout) for your table
Instead of using html table inside a TextView I've solved formatting normal text into a table like text, adding white spaces into the text to have a tidy structure.
This is the code:
String lines[] = getItem(position).toString().split("\n");
String print = "";
String parts[] = null;
String tmp = "";
int weight = 0;
for (int i=0; i < lines.length; i++) {
if (!lines[i].equalsIgnoreCase("")) {
parts = lines[i].split(":", 2);
tmp = "";
Paint textPaint = text.getPaint();
float width = textPaint.measureText(parts[0]);
float wslength = textPaint.measureText(" ");
for (int j = 0; j < (220 - Math.round(width))/Math.round(wslength); j++) {
tmp = tmp + " ";
}
if (print.equalsIgnoreCase("")) {
print = print + parts[0] + tmp + Html.fromHtml(""+parts[1]+"");
} else {
print = print + "\n" + parts[0] + tmp + parts[1];
}
}
}
You can find more explanations here:
http://blog.blupixelit.eu/convert-text-to-table-in-android-sdk/
Hope this helps.
How about change the way to just render table by webview, the other tags render by textview ? Recently i finish a demo to overcome this. So we first need separate<table> with others supported tags, which will like change "<p>**<p> blabala <table>balabla </table> blabla <p>**<p>" to three separated strings
<p>**<p> blabala
<table>balabla </table>
blabla <p>**<p>
Then only <table> included tags render by webview, the others by textview
And the result in android will be like:
<ScrollView>
<LinearLayout>
<TextView>
<WebView> --- let the webview ATMOST measured
<TextView>
So every thing goes fine as i think. check this commit for detail
HtmlTextView recently added basic support for HTML tables. It's limited, but will do the trick if all you have to worry about is <table>, <td>, <tr>, and <th>.
Related
From the following image, I want to extract number below text Arzt-Nr (654321161).
I've used OCR reader but it is extracting texts randomly not in a sequence, making it difficult to add a logic to extract no below "Arzt-Nr".
I've used following code but texts are not in sequence.
Is there any way to achieve this?
String text = "";
for (int i = 0; i < detectedItems.size(); i++) {
TextBlock item = detectedItems.valueAt(i);
String detectedText = item.getValue();
List<Line> lines = (List<Line>) item.getComponents();
for (Line line : lines) {
List<Element> elements = (List<Element>) line.getComponents();
for (Element element : elements) {
String word = element.getValue();
text = text + " " + word;
}
text += "\n";
}
}
Try to check a fixed length to the words after "Arzt-Nr" position, try also to check the pattern of the word founded.. for example if you need only numbers ecc...
Extract tsv output of image using tesseract and find the nearest text below the location of keyword. Also have a look at page segmentation modes of tesseract.
Link to Generating tsv
Link to use page segmentation
I am converting between "\n" to ":" in file .txt. And here, this is my paragraph before convert:
You can see that, between string "Hoa" and string "Đàm", it have one character " " and two character "\n". And this is my convert function:
private String convertData(){
String different = "~`!##$%^&*()-_=+*/\\\"'|]}{[:;?/.><,\n ";
StringBuilder data = new StringBuilder(tvData.getText().toString().trim());
for (int j = 0; j < data.length(); j++){
if(different.contains("" + data.charAt(j))) data.setCharAt(j, ':');
}
String convertData = data.toString().trim();
return convertData;
}
And this is result:
You can see behind character "\n" have a character, and it is not in string different.
Can anyone tell me what to do?
1 - Proceed like you're currently doing, but the \n (and the other escaped sequences).
So, use
String different = "~`!##$%^&*()-_=+*/'|]}{[:;?/.><, ";
2 - After the loop, use string.replace() to replace only the substring "\n" and the other sequences, i.e.: "\\" and "\"".
You could use a loop to replace the sequences, but you must replace 2 characters instead of one.
Basically, in the first loop you replace all the single characters.
Then, you replace the character sequences.
Notepad only supports Windows line endings (\r\n), so that must be what your file contains. Convert the file to Unix line endings (\n) using literally any program that is not Notepad, or add \r to your search pattern.
I am trying to get the text within my buttons to descend vertically like below. I'd prefer to have this happen within the main.xml file.
M
Y
T
E
X
T
I don't want the text to go sideways.
Use "\n" between the letters, that tells the compiler to add a return character between the characters.
A little helper function for this (i have no better idea, then adding the newlines):
public static final String rotateString(String str)
{
final StringBuilder sb = new StringBuilder();
for(int i=0; i<str.length(); i++)
sb.append(str.charAt(i)).append(i==str.length()-1? "" :'\n');
return sb.toString();
}
And then if you want to use it:
yourTextView.setText(rotateString("Hello Text"));
At the moment i could only test it on an emulator with 4.0.3, but it seems to work nicely.
I am using a text file that stores 3 columns of data, each having varying length.
This is the following code I have tried so far. I want each column to be left justified. For some reason it works if I use an asterisk and not if I try to manually insert a whitespace.
(line is reading in 3 words from my text file)
Attempt #2:
while((line = buf.readLine())!= null){
StringTokenizer st = new StringTokenizer(line);
int length = 12;
a = st.nextToken();
while (a.length() <= length)
{
a = a + "*";
}
b = st.nextToken();
while (b.length() <= length)
{
b = b + "*";
}
c = st.nextToken();
text.append(a + b + c + '\n');
}
This was my original attempt but this did not work either:
text.append(String.format("%-15s\t %-10s\t %-5s\t \n", a, b, c));
Any ideas would be greatly appreciated.
The key here is not to use just a single TextView for your three columns. You want to use a different TextView for each column and the control the formatting of each TextView individually. What type of layout are you using? If you switch to a table layout, it should be easy to left justify what ever you want. If your file will have a variable length, use a ListView which has three different text views in each row and then left justify each textview.
Here's my issue:
I have a database and it is full of episodes of a tv show. One column denotes the episode number. I want to display the episodes in a list like this:
Episode 1
Episode 2
Episode 3
etc.
I'm using my own adapter class that extends SimpleCursorAdapter to do this...
Since I had formatting errors I am using Android.R.layout.simple_list_item_1 and Android.R.id.text1
Basically the only reason I have a custom adapter is so I can do something like this:
textView.setText("Episode " + cursor.getString("column_for_episode_number");
The problem is, I get a list that looks like this:
Episode
1
Episode
2
Episode
3
When I try something like this(which worked in a different portion of my code):
String text = "Episode " + cursor.getString("blah");
text = text.replaceAll("\\n","");
I get the exact same list output :(
Why don't I use create a custom view with two textboxes next to each other? It is hard for me to get that to look pretty :/
text.replaceAll(System.getProperty("line.separator"), "");
There is a mistake in your code. Use "\n" instead of "\\n"
String myString = "a string\n with new line"
myString = myString.replaceAll("\n","");
Log.d("myString",myString);
Check if there is new line at the beginning before you replace and do the same test again:
for(int i=0; cursor.getString("blah").length()-1; i++)
{
if(cursor.getString("blah").charAt(i)=='\\n') <-- use the constant for the line separator
{
Log.i("NEW LINE?", "YES, WE HAVE");
}
}
Or use the .contains("\n"); method:
Check the xml for the width of the textview as well.
Why are you using getString() when you are fetching an integer? Use getInt() and then use Integer.toString(theint) when you are setting the values in a textview.
This could help you:
response = response.replaceAll("\\s+","");
It sounds like you are hitting wrapping issues rather than newline issues. Change this:
String text = "Episode " + cursor.getString("blah");
To this:
String text = "Episode" + cursor.getString("blah");
And see if that changes the output. Post your layout xml please?
this worked for my (on android 4.4):
(where body is a string with a newline entered from an EditText view on handset)
for (int i=0; i<body.length(); i++) {
if (body.charAt(i) == '\n' || body.charAt(i) == '\t') {
body = body.substring(0, i) + " " + body.substring(i+1, body.length());
}
}
have you tried
cursor.getString("blah").trim()