In my application need to send String to a printer, with structured String
Like first line is bold and middle of the printing page.
After draw line and make three column
Item code item quantity
set items in each row
String line="_____________________________________________________________________"+"\n";
String cl= " <H1>Company Name(CLPPL) <H1>"+"\n";
print=line+Html.fromHtml(cl)+line;
I am trying Like above
I am using Html but it is not working
Please Help Me how i can make this kind of the String
Thanks IN Advance
Embedded html in Java code.Use StringBuilder class to append string one by one.
Use hint
StringBuilder stringBuilder=new StringBuilder();
stringBuilder_month.append("<html><body>");
stringBuilder_month.append("<font color="+"#413839"+"><h3 align="+"center"+"<b1>");
stringBuilder_month.append(" ");
stringBuilder_month.append("Your Heading</b1>");
stringBuilder_month.append("    ");
stringBuilder_month.append("</body></html>");
Add html tags as you required.You need to modify it according to your needs. This code will solve your problem
Related
I am developing an calculator app. That app take input using edit text and shows the value in a textview using setText. Some times the input get bigger and it becomes hard to fit the output in a single line. That's why I need to spilt the output into multiple line.
I've used some line separator solutions from SO but nothing properly works for me.
Please give me a solution to show a way to show the output text into multiple line. Please don't flag as similar .
Note: All my input and output are number and they have no space or punctuation mark in them.
Thanks in Advance.
Im not allowed to comment yet. So have u heard of "Spanned"? Im Using kotlin and Assuming max items in one line is 20 .
Try This:
String yourString = "1010101110001101011010011100111000111100001110000";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(yourString);
for(int i = 0; i<yourString.length();i++){
if(i%20 == 0){
stringBuilder.insert(i,"<br/>");
}
}
Spanned spannedString = Html.fromHtml(stringBuilder.toString());
yourtextView.setText(spannedString);
feel free to give feedback.
I read the oracle escape sequence and realize that if I want to make a backspace in a textView I need to use "\b", the same way as we do for inserting a new line (/n). I've tried this line of code:
textView.setText("Hellos\bWorld");
Then, when I run the app, the textView shows this:
Hellos World
Intead of what I expected:
HelloWorld
I wish you can help me, how I can make a backspace within a textView. Any suggestion will be welcome.
Simplest method remove space in string is replace() method. It accepts two arguments 1st what word/char you want to search in string and 2nd what you want to replace with.
String dummy = "Hellos World";
String newText = dummy.replace("s ","");
textView.setText(newText);
//output > HelloWorld
String regex = "\\s*\\bis\\b\\s*";
String str = "Hellos World";
str = str.replaceAll(regex, "");
textView.setText(str);
\b doesn't work the way you are thinking. Basically,
\b allows you to perform a "whole words only". It matches the empty string at the beginning or end of a word.
So you can match \bword\b with \b. So to remove the character you want you either need to use substring or replace particular character.
my json is
{[{"key1":"value1","key2":"valu2"},{“ key3":"value3","key4":"valu4”}]}
How to change the above text as follows. Thank you for helping my friends
{"travel": [{"key1":"value1","key2":"valu2"},{ key3":"value3","key4":"value4}]}
Your original String is not a valid JSON for two reasons:
1 we can see some invalid quotes
2 elements inside a json object must have keys.
so assuming that your String is correct to achieve what you need in java you can do the following:
String string="{[{\"key1\":\"value1\",\"key2\":\"valu2\"},{\" key3\":\"value3\",\"key4\":\"valu4\"}]}";
StringBuilder stringBuilder=new StringBuilder(string);
stringBuilder.insert(1,"\"travel\":");
String json=stringBuilder.toString();
You just insert your key after the first character. Hope this will help.
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"));
How to search for one word in a big message in Android?
I have a text like "The sun always shines above the clouds". I wanna search for a single word, like "sun", and change it to an image. How to do this? Is there any way?
String word = "cat";
String text = "The cat is on the table";
Boolean found;
found = text.contains(word);
Regular Expressions in Java are the most flexible and powerful tools you can use to search and replace strings within other strings. Depending on where you display this data (eg. an HTML View perhaps?) you can replace the words with markup that can display an image or find the location in the string where you can break up elements to create TextViews vs ImageViews. On this latter case, another useful method within the String class might be the indexOf() or contains() methods.
To find the position of a given word in a string use the method
public int indexOf (String string)
For replacing strings with other strings you can use
public String replaceAll (String regularExpression, String replacement)
It is not clear what you mean with "I wanna search for single word like (sun) and change to an image"
An easy way is to use the String.replace method:
String source="The (sun) is shining.";
String replaced=source.replace('(sun)', '<img href="a_sun.png">');
See: http://javarevisited.blogspot.se/2011/12/java-string-replace-example-tutorial.html